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, ...
terdon's user avatar
  • 235k
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(+) ...
Ed Morton's user avatar
  • 29.2k
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 ...
Kusalananda's user avatar
  • 323k
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&...
Ed Morton's user avatar
  • 29.2k
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
Gilles Quénot's user avatar
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 ...
Kusalananda's user avatar
  • 323k
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 ...
Stéphane Chazelas's user avatar
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(+) ...
Kusalananda's user avatar
  • 323k
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(+) ...
terdon's user avatar
  • 235k

Only top scored, non community-wiki answers of a minimum length are eligible