Find fields and replace using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find fields and replace using awk
# 1  
Old 11-14-2013
Find fields and replace using awk

Code:
Using ksh
Code:
Var1=`awk -F";" {print $1}'  Input2.txt`
 
cat Input1.txt |  awk -F";"  '{$3="Var1"}' > Output.txt


Last edited by Roozo; 11-21-2013 at 02:15 AM..
# 2  
Old 11-14-2013
Assuming both the files Input.txt and Input2.txt have 1 row:
Code:
awk 'NR==FNR{var=$1;next}{$3=var}1' Input2.txt FS=; OFS=; Input1.txt > Output.txt


Last edited by Franklin52; 11-14-2013 at 05:01 AM..
# 3  
Old 11-14-2013
After executing the command
Code:
awk 'NR==FNR{var=$1;next}{$3=var}1' Input2.txt FS=; OFS=; Input1.txt > Output.txt



getting the below error,

Code:
Input1.txt : aster :not found 
Input1.txt :PAGE  :not found
Input1.txt:1233   :not found
Input1.txt: 4 :not found
Input1.txt:1222  :not found


Last edited by Roozo; 11-21-2013 at 02:17 AM..
# 4  
Old 11-14-2013
Try
Code:
awk 'NR==FNR{var=$1;next}{$3=var}1' Input2.txt FS=";" OFS=";" Input1.txt > Output.txt

# 5  
Old 11-14-2013
Are you trying to join three files?

If yes, something like this might work for the input that you posted:
Code:
awk -F'[; ]' '
        NR == FNR {
                i = $2
                next
        }
        !(j) {
                j = $1
                next
        }
        {
                $3 = j
                $5 = i
        }
        1
' OFS=';' config.txt Input2.txt Input1.txt

This User Gave Thanks to Yoda For This Post:
# 6  
Old 11-19-2013
Thanks Yoda, Your command was joining fields well!!

It’s taking field 2nd from config.txt and replacing $5 in Input1.txt. But Input2.txt file has two fields, for first line “1111 A” our command wants to compare it with config.txt and take “A DART” from config.txt to replace $5 in Input1.txt


1111 –input to replace
A -- to compare it with config.txt and take desire field, say ‘DART’

Example: Input files

Input1.txt:
Code:
aster;PAGE;1233;4;1222 

Input2.txt
Code:
1111 A
1234 B
2355    C

config.txt:
Code:
A DART
B MART
B KART
B KAR2
C HOME


So Output is:
Code:
aster;PAGE;1111;4;DART
aster;PAGE;1234;4;MART
aster;PAGE;1234;4;KART
aster;PAGE;1234;4;KAR2
aster;PAGE;2355;4;HOME

I Tried with below, but facing error
Code:
awk -F'[; ]' '
NR == FNR {
i = $2
p = $1
}
{
n=awk '$1==2 {print $2}' 
next
}
{
$2 = p
$3 = need
}
1
' OFS=';' Input2.txt config.txt Input1.txt


Last edited by Roozo; 11-19-2013 at 11:02 PM..
# 7  
Old 11-19-2013
For given input in #1 this will work

Code:
$ cat config.txt
A DART

$ cat input2.txt
1111 A

$ cat input1.txt
aster;PAGE;1233;4;1222

Code:
 
awk -F'[ ;]' '
             FNR == 1{
                       ++i
                     } 
               i == 1{
                       replace1 = $2
                     } 
               i == 2{
                       replace2 = $1
                     } 
               i == 3{
                       $3 = replace2
                       $5 = replace1
                       j=1
                     }j
              ' OFS=";" config.txt  input2.txt input1.txt

Resulting
Code:
aster;PAGE;1111;4;DART

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

find pattern matches in consecutive lines in certain fields-awk

I have a text file with many thousands of lines, a small sample of which looks like this: InputFile:PS002,003 D -1 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 6 6 -1 -1 -1 -1 0 509 0 PS002,003 PSQ 0 1 7 18 1 0 -1 1 1 3 -1 -1 ... (5 Replies)
Discussion started by: jvoot
5 Replies

2. Shell Programming and Scripting

Find and replace in awk

I have a file that I am trying to find a specific word, then replace text within that string. file TestA2015 TestB2016 Example. Replace TestB2016 to TestB0000, so if TestB is found replace the original "2016" to "0000". Thank you :). awk tried awk '{ sub(/TestB$/, "0000", $6) }1'... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Compare 2 files and find missing fields awk

Hello experts! I have 2 files. file1 is a list file containing uniquely names. e.g.: name1 number number name2 number number name5 number number name10 number number ... file2 is a data file arbitrary containing the names of file1 in paragraphs separated by "10" e.g. name4 ... (3 Replies)
Discussion started by: phaethon
3 Replies

4. Shell Programming and Scripting

Replace 0 with 1 in multiple fields with awk

Hello, I have the following input file: 1 3 3 2 3 3 4 0 4 0 5 4 5 2 2 0 5 3 4 0 6 0 3 2 I am trying to remove all zeroes in fields 2 and 4 and replace them with "1's" I tried the following, but it's not working awk -F"\t" '{ if (($2==0) || ($4==0) $2=1; $4=1; print $0 ) }' input ... (8 Replies)
Discussion started by: Rabu
8 Replies

5. Shell Programming and Scripting

Find and Replace in awk

Friends, I have more the thousand lines like this. check.cloud1.port=342 check.cloud2.port=5456 check.cloud3.port-4564 But we need to transfer it to _CHECK.CLOUD1.PORT_=342 _CHECK.CLOUD2.PORT_=5456 _CHECK.CLOUD3.PORT_=4564 Any one could pls help of this. Thanks in Advance ... (1 Reply)
Discussion started by: jothi basu
1 Replies

6. Shell Programming and Scripting

Find and Replace in multiple fields using awk

Hi, Say I have a record "1|22| | |". In which the third and fourth fields are <space> alone. I have to replace the <Space> with <null>. Input: "1|22| | |" --> "1|22|<space> |<space> |" Expected output: "1|22|||" --> "1|22|<null> |<null>|" I tried: echo "1|22| | |" | awk -F... (4 Replies)
Discussion started by: machomaddy
4 Replies

7. Shell Programming and Scripting

AWK: merge two files and replace some fields

Need some code tweak: awk 'END { for (i=1; i<=n; i++) if (f2]) print f2] } NR == FNR { f2 = $1] = $0 next } $1 in f2 { delete f2 }1' FS=, OFS=, 2.csv 1.csv > 3.csvfile 1.csv have: $1,$2,$3,$4,$5,$6,$7,$8,$9...... file 2.csv have: $1,$2,$3,$4,$5,$6 (2 Replies)
Discussion started by: u10
2 Replies

8. Shell Programming and Scripting

use awk to replace empty fields with the latest nonempty field

Hi suppose I have a csv file like this count,1977,1978,1979 usa, , , blue japan, red, yellow,green india, , yellow,blue china, blue, yellow, green I want the output to be(replace everything, including empty data, with the most recent data): ... (1 Reply)
Discussion started by: grossgermany
1 Replies

9. Shell Programming and Scripting

awk find/replace

Greetings all. I have web site that has long option and switch lists. When I insert something new into these files, the lists need to be reordered. IE: 1 => apple 2 => pear 3 => bannana 4 => orange --------------------- Add grape as #2 1 => apple 2 => grape 3 => pear 4 =>... (2 Replies)
Discussion started by: RobertSubnet
2 Replies

10. UNIX for Dummies Questions & Answers

How do I read/find/replace fields in a csv datafile?

hello. I'm somewhat a novice here so please be patient. My stumbling block when loading csvs into ORACLE tables is this: I need to read a csv datafile, check several fields in each line, and if any of stated fields contain A ZERO only then replace it with a null/blank character. I had a... (9 Replies)
Discussion started by: MrCarter
9 Replies
Login or Register to Ask a Question