I need to add # at the beggining of every line in unix file between line numbers 115 and 315, how to do it ??
I tried the below command:
awk '{print "#" $0;}'>filename
but this added # to every line of file. The file has more than 1000 lines. I only want # to be added between line no 115 and 315. Kindly help
Thanks,
Sen
Try
sed -i '115,315 s/^/#/' filename
This will add a # to the beginning of lines numbered 115 - 315.
Related
I have a file which has 109 lines.
I perform the two operations on the line shown below.
# Delete line 74
sed -i '74d' Test.txt
# Add the entry to line 109
sed -i "109iThis is the string" Test.txt
I see line 74 getting deleted from my Test.txt, but for some reasons, now my Test.txt has only 108 lines, and I don’t see the This is the string being added to line 109.
I am not sure what the error is. How can I fix it?
You may use this POSIX sed command:
sed -i.bak '74d; $ a\
This is the string
' file
This will delete 74th line from file and append a line in the end and will save changes inline.
Note that this will work with gnu-sed as well.
Jonathan already mentioned the potential issues with using sed -i (non-standard, behaves in different ways when supported depending on implementation, etc.). Avoid them by using ed to edit files:
ed -s Test.txt <<EOF
109a
This is the string
.
74d
w
EOF
Note how this appends, and then deletes. Because ed acts on entire files, not a stream of lines, commands to act on specific lines can be in any order.
If you remove a line, the file has only 108 lines left. Correct your second command accordingly:
sed -i "108iThis is the string" Test.txt
Line number 109 does not exist (you removed one, 109-1=108), you must add it before you can enter text into it.
Solution:
sed -i '$ a <text>' Test.txt
The new line will be added with the selected text.
I have a set of text files and I'd like to display lines 12-14 by running a bash script on each file.
For one of the files, this works:
tail -14 | head -11
But since other files have different lengths, I cannot run the same script on them.
What is the command I'm looking for to output lines 12-24 of the text file?
Use sed with -n argument
sed -n 12,24p <FILENAME>
For a funny pure Bash (≥4) possibility:
mapfile -t -s 11 -n 13 lines < file
printf '%s\n' "${lines[#]}"
This will skip the first 11 lines (with -s 11) and read 13 lines (with -n 13) and store each line in a field of the array lines.
Using awk:
awk '12<= NR && NR <= 24' file
In awk, NR is the line number. The above condition insists that NR be both greater than or equal to 12 and less than or equal to 24. If it is, then the line is printed. Otherwise, it isn't.
A more efficient solution
It would be more efficient to stop reading the file after the upper line limit has been reached. This solution does that:
awk 'NR>24 {exit;} NR>=12' file
So I am looking to grep the number of a given character in every line of a very very large file (50Gb). I need to get output for each line that contains the Character ? or . the number of occurrences per line as well as the line number. Any ideas on how to do this simply from bash? I know the max number of either char is 10 per line, min is 0. This is from a old, poorly written custom DB that I need to transform and the original dev used the periods and question marks as delimiters.
The following awk command will print the line number, the number of ?s found and the contents of the line containing the ?:
awk -F? '/?/{print NR,NF-1,$0}' file
This will output a line-by-line count of ? or . chars in the file preceeded by the line number:
line_no=1;
while read line
do
echo $line_no $((`echo $line | sed 's/[^?.]//g' | wc -m`-1))
line_no=$(($line_no+1));
done < "your_file.txt"
I have a file having 200 lines and I want to remove 5 long lines( each line contains special characters).
$cat abc
............
comments[asci?_203] part of jobs where to delete
5 similar lines
.....
I tried sed to remove these 5 lines, using line numbers(nl) on the file, but did not work.
Thanks
Have you tried to remove the lines with awk? This is untested with special characters, but maybe it could work
awk '{if (length($0)<55) print $0}' < abc
Replace 55 with the maximum line length you want to keep
I am trying to copy part of a .txt file from the line number n to the line number n+y (let's say 1000 to 1000000).
I tried with operators and sed, and it failed. Here's the command I tried:
sed -n "1000, 1000000p" path/first/file > path/second/file
if you know how many lines are in your source file (wc -l) you can do this .. assume 12000 lines and you want lines 2000 - 7000 in your new file (total of 5000 lines).
cat myfile | tail -10000 | head -5000 > newfile
Read the last 10k lines, then read the 1st 5k lines from that.
sed command should work fine, replace double quotes with single quotes.
sed -n '1000, 1000000p' path/first/file > path/second/file