sed help - search/copy from one file and search/paste to another

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed help - search/copy from one file and search/paste to another
# 1  
Old 08-17-2011
sed help - search/copy from one file and search/paste to another

I am a newbie and would like some help with the following -

Trying to search fileA for a string similar to -
AS11000022010   30.4   31.7   43.7   53.8   60.5   71.1   75.2   74.7   66.9   56.6   42.7   32.5   53.3

I then want to replace that string with a string from fileB -
AS11000022010  30.92  32.42  44.44  54.30  60.92  71.42  75.55  74.89  67.07  56.72  42.99  32.89  53.71

There are multiple lines of data so it would be every instance of AS1100002.

Thanks in advance

Last edited by Franklin52; 08-19-2011 at 03:08 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 08-17-2011
Not sure about using sed for this, but here is a solution using awk:

awk 'NR==FNR { K[$1]=$0; next }
{ if($1 in K) $0=K[$1]; print }' fileB fileA

Or if you only want to search/replace for a given value:

awk -vN=AS11000022010  'NR==FNR { if($1 == N)K[$1]=$0; next }
{ if($1 in K) $0=K[$1]; print }' fileB fileA

Last edited by Chubler_XL; 08-17-2011 at 11:19 PM..
# 3  
Old 08-18-2011
Thanks Chubler.

The code
awk -vN=AS11000022010  'NR==FNR { if($1 == N)K[$1]=$0; next }
{ if($1 in K) $0=K[$1]; print }' fileB fileA

will produce one line of variables.

Im still trying to figure out

1) how to return multiple lines of variables. Basically need to figure out how to include a wildcard in the AS110000* using AWK.

2) instead of printing out on the screen - what is the correct code for embedding the output it in a file.

# 4  
Old 08-18-2011
For wild card matching use awk ~ operator instead of ==
Not sure what you mean by embedding output, but you can redirect the output of awk to a file with > filename, like this:

awk -vN=AS110000*  'NR==FNR { if($1 ~ N)K[$1]=$0; next }
{ if($1 in K) $0=K[$1]; print }' fileB fileA > fileC

# 5  
Old 08-19-2011
Thanks for the help. It works perfectly.

While reading through the script I am having trouble figuring out what the 'K' does?
# 6  
Old 08-21-2011
K is used as an associative array here.

root@bt:/tmp# cat file
Linux1    Ubuntu
Linux2    Fedora
Linux3    BackTrack

root@bt:/tmp# awk '{K[$1]=$2} END{ for(i in K){print "Key : "i" & Value : "K[i]} }' file
Key : Linux1 & Value : Ubuntu
Key : Linux2 & Value : Fedora
Key : Linux3 & Value : BackTrack


Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Writing a search, copy and paste program

Hello, Can some one help me by writing me the script of a program that does the following simple functions on a Linux Mint: 1. it runs in the background all the time, doing nothing except of checking if there is any external device mounted. 2. when the device is detected, it copies files... (1 Reply)
Discussion started by: Black_Ardilla
1 Replies

2. Shell Programming and Scripting

Search the pattern and copy in to the file

my qn is i have one file contains some junk data and some hexa valueslike hex.txt sdfjhjkh 0x1233jkfhgjfhgajk;gha 0xacdd jkgahfjkgha;sjghajklgha;gh aghfjkgh;a 0xccc jhfjkhsd ox23cd 0x456 jkhdfjhjkafh like this now iwant a script like to separate the hex values and paste into the... (12 Replies)
Discussion started by: siva.hardwork
12 Replies

3. Shell Programming and Scripting

search a word and copy the file

Hi need help with a script or command My requirement is - I need to do a "ls -ltr tcserver*.syslog" files in /tmp, direct the output to a file ls -ltr tcserv*.syslog | grep "Jan 31" | awk '{printf "\n" $9}' > jandat.logs -Then open each file in above list and search for string/word,... (4 Replies)
Discussion started by: karghum
4 Replies

4. Shell Programming and Scripting

sed copy paste

Hello, I have this path and file: /dir/dir/dir/dir/dir/dir/dir/dir/dir/THIS_SPOT/fle.txt I want to end up with: /dir/dir/dir/dir/dir/dir/dir/dir/dir/THIS_SPOT/fle.txtTHIS_SPOT Take the dir after the 10th slash, add a tab at the end and paste the dir it copied. Thanks (4 Replies)
Discussion started by: crowman
4 Replies

5. Shell Programming and Scripting

Search multiple strings on a file and copy the string next to it

I tried awk for this, but failed <or my code is not correct? I dont know>. Can anyone help me on this? ---------- Post updated at 08:34 PM ---------- Previous update was at 08:29 PM ---------- my working file looks like this: <empty> <empty> <empty> NAME :ABC AGE :15 GENDER... (6 Replies)
Discussion started by: kingpeejay
6 Replies

6. Shell Programming and Scripting

Search, copy and paste

Can i search in a file for more than one string at a time? And copy the next string after that and paste it in column style? Is it possible? Thanks! (4 Replies)
Discussion started by: kingpeejay
4 Replies

7. Shell Programming and Scripting

Search and copy to a new file-Help

Hi All, UNKNOWN ftpuser "CWD" dms-imrm/Delasco_Invoices_DayForward_Scan" 250 - UNKNOWN ftpuser "PWD" 257 - UNKNOWN ftpuser "CWD Private" 250 - UNKNOWN ftpuser "PWD" 257 - UNKNOWN... (7 Replies)
Discussion started by: Tuxidow
7 Replies

8. UNIX for Advanced & Expert Users

File extension search and copy

Hi need to know if we can write a shell script to find files for a particular format;s ie both .csv and .txt in a particular folder and then copy them to a new folder on a dialy basis. Does anyone know how this can be accomplished? Thanks, Sandeep (20 Replies)
Discussion started by: bsandeep_80
20 Replies

9. Shell Programming and Scripting

Search for strings & copy to new file

Hi All, I am just learning shell programming, I need to do the following in my shell script. Search a given log file for two\more strings. If the the two\more strings are found then write it to a outputfile else if only one of the string is found, write the found string in one output... (2 Replies)
Discussion started by: amitrajvarma
2 Replies

10. UNIX for Dummies Questions & Answers

Search and paste a line..

Hi everyone, I've never used Unix ever in my life and I just got a summer job that requires me to manipulate Unix =( I'm currently encountering a problem... I need to search for Keyword in a text file, and if I find Keyword , I'll have to output the whole line that contains Keyword into a new... (4 Replies)
Discussion started by: SeanDM
4 Replies
Login or Register to Ask a Question