Replace some strings keeping others


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace some strings keeping others
# 1  
Old 10-12-2014
Replace some strings keeping others

I want to replace strings in test2 according to test1 table. In doing so, I`m losing records that I dont need to replace, please suggest modifications.

what i have

Code:
 
$ cat > test1
a b
c d
 
$ cat > test2
a
a
a
d
d
 
what i tried 
 
$ awk ' BEGIN {FS=OFS=" "} FNR==NR{a[$1]=$2;next} $1 in a{print a[$1]}' test1 test2
b
b
b



what i want

Code:
 
 
b
b
b
d
d



# 2  
Old 10-12-2014
Taking your request at face value:
Code:
awk 'FNR==NR{a[$1]=$2;next} {print (($1 in a) ? a[$1] : $1);}' test1 test2

This User Gave Thanks to Aia For This Post:
# 3  
Old 10-12-2014
thank you Aia, I have another question but I should create a new post for it..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to replace strings

Hi.. I have a file that has the following content : 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... (5 Replies)
Discussion started by: Hermione Grange
5 Replies

2. 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

3. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

4. Shell Programming and Scripting

Replace date value with another value keeping all as is

Hi forum. How do I change the following date value with another value (while keeping the rest of the line) using sed? The date values can change so I need a general sed command to change the date value within the first quotation marks only. Date values will be coming from 2 different files.... (2 Replies)
Discussion started by: pchang
2 Replies

5. 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

6. 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

7. 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

8. UNIX for Dummies Questions & Answers

Help to replace character strings

Hello Can Any1 help me. I want to replace a specific character string inside a file at a specific location with a particular character with the help of a command or a shell script. The tr command replaces a specific character with another for all the occurences of that character in the file. I... (5 Replies)
Discussion started by: rahulrathod
5 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