Awk or Sed to Remove All Text After X Occurence in Each Line of File

awk or sed to remove all text after x occurence in each line of file

EDIT: As per OP's comment editing my code now as follows.

echo "/value1/value2/value3/value4/something/whatever" | awk -F"/" '{NF=4} 1' OFS="/"

Since you have not shown samples of input and output so based on your statement following simple awk may help you here.

awk '{sub(/value3.*/,"value3")} 1' Input_file

How to delete second occurrence from a line using Unix

sed can do that using s/office//2. On top of that, you can combine the entire pipeline into a single command.

sed -n '/Mandy/s/office//2p' file.txt

Explanation:

In above command there are 2 sed commands.

  1. /Mandy/ runs the next command only on lines containing Mandy.

  2. s/office//2p deletes (substitute with the empty string) the 2nd occurrence of office in those lines and prints them afterwards.

Delete everything after the nth mention of a character in bash

Using cut you can do this:

cut -d_ -f1-3 file

ASSI-3_2 scaf0270669_20068.102
ASSI-4_3 scaf0189112_70083.538
ASSI-5_4 scaf0083789_70072.963
ASSI-8_7 scaf0423760_50193.589
ASSI-11_10 scaf0285971_60192.428
ASSI-12_11 scaf0409557_70062.641
ASSI-13_12 scaf0430981

Or using awk:

awk -F_ 'NF>3{$0=$1 FS $2 FS $3} 1' file

ASSI-3_2 scaf0270669_20068.102
ASSI-4_3 scaf0189112_70083.538
ASSI-5_4 scaf0083789_70072.963
ASSI-8_7 scaf0423760_50193.589
ASSI-11_10 scaf0285971_60192.428
ASSI-12_11 scaf0409557_70062.641
ASSI-13_12 scaf0430981

How to replace mth to nth occurance of a string in bash using sed?

1st solution: IMHO if you ask me simple solution then I would go for awk. If your Input_file contains only 4 fields then simply do, simply assign the values to 3 fields.

awk '{$1=$2=$3="****"} 1'  Input_file

2nd solution: With sed(this could be the way OP was trying to write). Using sed's capability to use temp buffer to store matched regex and later replace it with **** while substitution.

sed 's/\([^ ]*\) \([^ ]*\) \([^ ]*\) \(.*\)/**** **** **** \4/'  Input_file

3rd solution: Use rev to print Input_file reverse then catch only first(which is actually last field in Input_file) and then print 3 times **** and again print it reverse which will print it now in its actual form :)

rev Input_file | sed 's/\([^ ]*\).*/\1 **** **** **** ****/' | rev

4th solution: More generic solution, where person can give range of field numbers from which field to which field person wants to change value to **** then try following (from and to are the variables which could be set by person to change values according to field numbers).

awk -v from="1" -v to="3" '{for(i=from;i<=to;i++){$i="****"}} 1' Input_file

awk Extract Text Nth Occurrence Square Brackets (Containing Line Break In File Text)

how to extract the text between [ and ] with the given record number

You may try this gnu-awk command that will work irrespective of presence of line break between bracket pairs

awk -v n=2 -v RS='\\[[^]]*]' 'RT && NR == n {print substr(RT, 2, length(RT)-2)}' file

{G,H,I}

Since we are using custom RS of [...] it will print correct record no matter if 2nd pair of [...] is in first line or second line.



Related Topics



Leave a reply



Submit