I want to delete third line of file named file2. Its running successfully.No Issues with it.
sed '3d' ../log/file2.txt > ../log/file8.txt
Now i want to use variable VAR1 (where VAR1=2).
I wrote the following command
sed "${VAR1+1}d" ../log/file2.txt > ../log/file8.txt
but this command deleting the 2nd line. No error while executing the command.But I am not getting expected output
Please help me
How to use sed in this command to get the correct line to be deleted
Try
sed "$((VAR1+1))d" ../log/file2.txt
Remove a line using variable and awk
var1=4
awk NR!=v v=$var1 ../log/file2.txt > ../log/file8.txt
This removes the 4th line in file2
To make it more robust, use qutes
awk 'NR!=v' v="$var1" file
Related
I use sed to remove all lines starting from "HETATM" from the input file and cat to combine another file with the output recieved from SED
sed -i '/^HETATM/ d' file1.pdb
cat fil2.pdb file1.pdb > file3.pdb
is this way to do it in one line e.g. using only sed?
If you want to consider awk then it can be done in a single command:
awk 'FNR == NR {print; next} !/^HETATM/' file2.pdb file1.pdb > file3.pdb
With cat + grep combination please try following code. Simple explanation would be, using cat command's capability to concatenate file's output when multiple files are passed to it and using grep -v to remove all words starting from HETATM in file1.pdb before sending is as an input to cat command and creating new file named file3.pdb from cat command's output.
cat file2.pdb <(grep -v '^HETATM' file1.pdb) > file3.pdb
I'm not sure what you mean by "remove all lines starting from 'HETATM'", but if you mean that any line that appears in the file after a line that starts with "HETATM" will not be outputted, then your sed expression won't do it - it will just remove all lines starting with the pattern while leaving all following lines that do not start with the pattern.
There are ways to get the effect I believe you wanted, possibly even with sed - but I don't know sed all that well. In perl I'd use the range operator with a guaranteed non-matching end expression (not sure what will be guaranteed for your input, I used "XXX" in this example):
perl -ne 'unless (/^HETATM/../XXX/) { print; }' file1.pdb
mawk '(FNR == NR) < NF' FS='^HETATM' f1 f2
I have a text file and I would like to only delete the first character of the text file, is there a way to do this in shell script?
I'm new to writing scripts so I really don't know where to start. I understand that the main command most people use is "sed" but I can only find how to use that as a find and replace tool.
All help is appreciated.
You can use the tail command, telling it to start from character 2:
tail -c +2 infile > outfile
You can use sed
sed '1s/^.//' startfile > endfile
1s means match line 1, in substitution mode (s)
^. means at the beginning of the line (^), match any character (.)
There's nothing between the last slashes, which means substitute with nothing (remove)
I used to use cut command to do this.
For example:
cat file|cut -c2-80
Will show characters from column 2 to 80 only.
In your case you can use:
cat file|cut -c2-10000 > newfile
I hope this help you.
[]s
You can also use the 0,addr2 address-range to limit replacements to the first substitution, e.g.
sed '0,/./s/^.//' file
That will remove the 1st character of the file and the sed expression will be at the end of its range -- effectively replacing only the 1st occurrence.
To edit the file in place, use the -i option, e.g.
sed -i '0,/./s/^.//' file
or simply redirect the output to a new file:
sed '0,/./s/^.//' file > newfile
A few other ideas:
awk '{print (NR == 1 ? substr($0,2) : $0)}' file
perl -0777 -pe 's/.//' file
perl -pe 's/.// unless $done; $done = 1' file
ed file <<END
1s/.//
w
q
END
dd allows you to specify an offset at which to start reading:
dd ibs=1 seek=1 if="$input" of="$output"
(where the variables are set to point to your input and output files, respectively)
The following command doesn't repeat the process for each occurence in one line...
input_file.txt :
<!--:nl-->hond <span>bob</span><!--:fr-->chien <span>bob</span><!--:nl-->kat<!--:fr-->chat
<!--:nl-->hond<!--:fr-->chien<!--:nl-->kat<!--:fr-->chat
wrong sed command :
sed -e 's/\(\<\!--\:nl\--\>\).*\(\<\!--\:fr\--\>\)/\1\2/g' input_file.txt > output_file.txt
current output_file.txt result :
<!--:nl--><!--:fr-->chat
desired output_file.txt result :
chien <span>bob</span>chat
chienchat
[EDIT] hond, chien, kat and chat may have HTML tags around them that need to be kept...
You can use this sed:
sed 's/<!--:nl-->[^<]*<!--:fr-->//g' file
Following awk may also help you in same.
awk -F'<!--:nl-->|<!--:fr-->' '{print $3$5}' Input_file
Explanation: Simply making strings <!--:nl--> OR <!--:fr--> as field separators and then printing 3rd and 5th columns of the line(as per your output required).
I have a file (foo.txt) containing the following:
some-text 0
I use the following sed-command to replace the 0 with a 1:
search_text="some-text";
sed "s/${search_text} 0/${search_text} 1/" -i foo.txt;
This results in foo.txt containing:
some-text 0
some-text 1
How can I get it to replace the found line instead of appending a new line?
It occurs with GNU sed version 4.2.1 on SL06.
If you like to try awk
awk '/some-text/ {$2=1} 1' file
do you try
search_text='some-text'
sed -e "s/\(${search_text}\) 0/\1 1/" -i foo.txt
using group pattern instead of twice the search_text
which shell are you using (cause i see ; like c end of line not often used on several line in shell) ?
How do you replace a blank line in a file with a certain character using sed?
I have used the following command but it still returns the original input:
sed 's/^$/>/' filename
Original input:
ACTCTATCATC
CTACTATCTATCC
CCATCATCTACTC
...
Desired output:
ACTCTATCATC
>
CTACTATCTATCC
>
CCATCATCTACTC
>
...
Thanks for any help
Here is a way with awk. This wouldn't care if you have spaces or blank lines:
awk '!NF{$0=">"}1' file
NF stands for number of fields. Since blank lines or lines with just spaces have no fields, we use that to insert your text. 1 triggers the condition to be true and prints the line:
Test:
$ cat -vet file
ACTCTATCATC$
$
CTACTATCTATCC$
$
CCATCATCTACTC$
$
$ are end of line markers
$ awk '!NF{$0=">"}1' file
ACTCTATCATC
>
CTACTATCTATCC
>
CCATCATCTACTC
>
You may have tabs or white spaces in your filename' empty lines, try the following:
sed 's/^\s*$/>/' filename
You may have whitespace in your input. First thing to try is:
sed 's/^[[:blank:]]*$/>/' filename
The following code should work:
sed -i 's/^[[:space:]]*$/string/' foo
What's missing here is the escape character. This will work for you.
sed 's/^$/\>/g' filename
And if you need to delete the empty lines and print others, Try
sed '/^$/d' filename