4
votes
Accepted
Replace last word occurrence in file
Reverse the file, comment out the first, and reverse the file back again:
$ tac temp.sh | sed '0,/^Abc/{s/^Abc/#&/}' | tac
Abc 123 Abc
Sdf 2
Abc
#Abc
Utyr
Qww
That means "start at line 0, ...
2
votes
Accepted
Duplicate part of a line to another part
Using any awk:
$ awk 'split($1,a,/[>.]/) > 1{ $NF=a[2] $NF } 1' file
>IxoscaEVm2293881t1.p1 type:complete len:255 gc:Universal IxoscaEVm2293881t1:13-219(+)
...
2
votes
Fitering rows of one CSV file based on two values from another file in Bash
Assuming you have two CSV files as follows:
$ cat file1
Chr_Name,h,j,start_pos,end_pos
Chrk,10,20,1010,1025
Chrk,20,10,1020,1040
ChrM,10,10,50,120
$ cat file2
Chr_Name,position
Chrk,1030
ChrM,70
You ...
2
votes
Split a CSV file based on second column value
Using any awk:
$ awk -F',' '
NR==1 { print > "file_under"; print > "file_over"; next }
{ print > ( "file_" ($2 < 60 ? "under" : "over&...
2
votes
Replace last word occurrence in file
With awk (no pipe(s)):
awk -v str=Abc '
NR==FNR{if ($0 == str) nr_str=NR; next}
{print (FNR == nr_str) ? "#"$0 : $0}
' file file
Output
Abc 123 Abc
Sdf 2
Abc
#Abc
Utyr
Qww
2
votes
Replace last word occurrence in file
$ sed -e 'H; $!d' -e 'g; s/\n\(.*\n\)\(Abc\)/\1#\2/' file
Abc 123 Abc
Sdf 2
Abc
#Abc
Utyr
Qww
This reads the whole file into the hold space of sed, modifies it and outputs the modified text.
The ...
1
vote
Replace last word occurrence in file
One approach is to process the file as a whole:
perl -pi -0777 -pe 's/.*\K^Abc/#$&/ms' temp.sh
Or without regexp and assuming the last occurrence of Abc at the start of the line is not on the ...
1
vote
Duplicate part of a line to another part
With sed:
$ sed 's/^>\(\([^.]*\).*\):/>\1\2:/' file
>IxoscaEVm2293881t1.p1 type:complete len:255 gc:Universal IxoscaEVm2293881t1:13-219(+)
...
1
vote
Duplicate part of a line to another part
Here's one way:
$ perl -pe 'if(/^\s*>(\S+?)\.p1/){$name=$1; s/(.*):/$1$name:/}' file.pep
>IxoscaEVm2293881t1.p1 type:complete len:255 gc:Universal IxoscaEVm2293881t1:13-219(+)
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
text-processing × 8338awk × 2924
sed × 2319
shell-script × 1105
grep × 840
bash × 700
linux × 573
shell × 374
regular-expression × 365
csv × 362
perl × 340
command-line × 331
scripting × 280
files × 218
columns × 181
bioinformatics × 179
text-formatting × 175
sort × 172
join × 134
cut × 108
xml × 100
replace × 95
json × 89
numeric-data × 88
text × 78