Using awk to replace strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk to replace strings
# 1  
Old 03-09-2015
Using awk to replace strings

Hi.. I have a file that has the following content :
Code:
abc 213 24 213
pqr 456#34 678
xyz 213 45%213

i need to write an awk script that will replace the second 213 in all the lines, if it is present. The IFS can not be specified and can be random.

The number of lines in the file and the columns in each line are not fixed.

Please suggest a solution. Thanks!

Last edited by Franklin52; 03-09-2015 at 10:21 AM.. Reason: Please use code tags
# 2  
Old 03-09-2015
Hi,
Use code tag for input file...
You want replace the second 213 but with what ?
to do this, it's easiest with sed:
Code:
sed -e 's/[^0-9]213[^0-9]/value_replacement/2' inputfile

Regards.
# 3  
Old 03-09-2015
i need to replace it with 243. Also, it is a part of an awk script so need to use awk alone. Need some generalized script to specify the substring at a certain position alone in a line.
# 4  
Old 03-09-2015
It's really no easy with awk, but if you use gawk, you can do it with:
Code:
 gawk '{A=gensub(/([^0-9])123([^0-9])/,"\\1\n123\n\\2",$0); A=gensub(/([^\n0-9])123([^\n0-9])/,"\\1243\\2",A); gsub(/\n/,"",A); print A}'

Regards.
# 5  
Old 03-09-2015
How about
Code:
awk '{sub (/213/, "\001");sub (/213/,"243"); sub (/\001/, "213")} 1' file3
abc 213 24 243
pqr 456#34 678
xyz 213 45%243

# 6  
Old 03-09-2015
Please provide a desired output sample.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

2. Programming

How to replace the complex strings from a file using sed or awk?

Dear All, I am having a requirement to find the difference between 2 files and generate a discrepancy report out of it as an html page. I prefer using diff -y file1 file2 since it gives user friendly layout to know any discrepancy in the record and unique records among the 2 file. Here's how it... (12 Replies)
Discussion started by: Badhrish
12 Replies

3. Shell Programming and Scripting

Replace the strings in file

Hello members, I been following this forums since very long time. I need to do one job. In my script I am evaluating one variable, lets say n=100. Now i have xml file inside which i need to replace the numbers in the desired lines with the evaluated number(n) +1. For example let's say... (4 Replies)
Discussion started by: mailtosaiki
4 Replies

4. Shell Programming and Scripting

replace newline between strings

I'm trying to figure out a way to replace newlines after a unique string is read, all the way until that string occurs again. The input file looks something like this: 02/11/11 22:48:00 This is the first line This is the second line This is the third line 02/11/11 22:49:00 This is the... (8 Replies)
Discussion started by: acruhl
8 Replies

5. Shell Programming and Scripting

Using sed to replace two different strings?

Hey everyone! Simple question - I am trying to use sed to replace two different strings. As it stands I can implement this as: sed -i 's/TIMEOUT//g' sed -i 's/null//g' And it works. However, is it possible to shrink that down into a single command? Will there be any performance benefits? (3 Replies)
Discussion started by: msarro
3 Replies

6. Shell Programming and Scripting

Replace Strings with sed or awk

Hello i need some help with the usage of sed. Situation : 2 textfiles, file.in , file.out In the first textfile which is called file.in are the words for the substitution. Every word is in a new-line like : Firstsub Secondsub Thridsub ... In the second textflie wich is called file.out is... (5 Replies)
Discussion started by: Kingbruce
5 Replies

7. Shell Programming and Scripting

AWK find/replace 2 strings in one shot

Friends, I have a file with contents like: interface Serial0/4/0/0/1/1/1/1:0 encapsulation mfr multilink group 101 interface Serial0/4/0/0/1/1/1/2:0 encapsulation ppp multilink group 101 I just have to repace mfr with ppp and ppp with mfr in a single shot. I tried using... (4 Replies)
Discussion started by: shrijith1
4 Replies

8. Shell Programming and Scripting

Replace fixed strings only

Hi All, I just need to do find and replace in a file.... say for eg I have the input file like below: in.txt ##### oldtextoldtext oldtext oldtext oldtext oldtext123 oldtext- oldtext I need to replace oldtext to newtext... my output file should come like below.. out.txt... (9 Replies)
Discussion started by: askumarece
9 Replies

9. Shell Programming and Scripting

To replace string between two strings

how do i replace a string within two strings ?. i have a string called my_string i.e my_string="GACAHX04GAC010000000001DDELTA 0001DAT00001320SLTZ" i need to replace all characters between DAT and SLTZ with zeros, the number of characters between these strings might vary . i.e output ... (1 Reply)
Discussion started by: amit1_x
1 Replies

10. Shell Programming and Scripting

replace strings in a file

Hi I want to change the following passwd: files nis group: files nis in /etc/nsswitch.conf to be passwd: files compat group: files compat I tried cp -p nsswitch.conf nsswitch.conf.old (3 Replies)
Discussion started by: melanie_pfefer
3 Replies
Login or Register to Ask a Question