awk - change more data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - change more data
# 1  
Old 06-17-2011
awk - change more data

Origin file has got 180 fields. I have to change olny 12th,18th and 52nd one.
In a file new.txt I've got those changes in 4 fields (first one is index):
Quote:
23535|000000|928283|001010|
I know how to change ONE field with awk array. But my question is: could it be changed more than one field in one single step?

ps: one solution, of course, is to make three input files (index + each change in a separate file) and run awk 3 times;
pps: or, have 3 input files for one awk script; but I wonder if I can do this without cutting my "new.txt" Smilie

Franci
# 2  
Old 06-17-2011
Assuming the first field of your original file is the index:
Code:
awk -F"|" '
NR==FNR{
  a[$1]=$2; b[$1]=$3; c[$1]=$4
  next
} 
a[$1]{
  $12=a[$1]; $18=b[$1]; $52=c[$1]
}1' OFS="|" new.txt origin.file

# 3  
Old 06-17-2011
thx

Thx a lot, Franklin.
It works, of course. But I have a question more: I want to get as output only changed data.
I tried to do it, but no success Smilie
# 4  
Old 06-17-2011
Something like this?
Code:
awk -F"|" '
NR==FNR{
  a[$1]=$2; b[$1]=$3; c[$1]=$4
  next
} 
a[$1]{
  $12=a[$1]; $18=b[$1]; $52=c[$1]
  print
}' OFS="|" new.txt origin.file

# 5  
Old 06-17-2011
I'm so confused right now, cause I'm 100% sure that it worked. Now it doesn't any more... Or I was dreaming Smilie
Output is the same as input (origin.file)?!?
Also second version works the same.
It's one mistake:
Quote:
}'1 OFS="|" new.txt origin.file
...missed "1"
Can you explain me, please, more about how this script works, what for is this "1"? Thx a lot.
# 6  
Old 06-17-2011
The '1' is similar to {print}, the '1' in the first solution is a command to print all the lines.
The print command in the 2nd solution prints only the changed lines.

If it doesn't work you should post an example of both files and the desired output.

Please use code tags instead of quote tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change Data of first ROW

My Input : A.txt 1@A 2@B 3@C 1 4 5 2 4 5 3 4 5 B.txt 1 2 3 A B C 1 4 5 2 4 5 3 4 5 (3 Replies)
Discussion started by: asavaliya
3 Replies

2. Shell Programming and Scripting

Change Data base on Column

Base of last two column i want to change may data if Last two Column have A and C then Copy Column $4 to Column $3. Input :- DD142 0_1 DD142_A DD142_B A B DD142 1_1 DD142_B DD142_C B C DD142 2_1 DD142_A DD142_C A C DD142 3_1 DD142_A A DD142 3_2 DD142_A A DD142 4_1 DD142_B B ... (4 Replies)
Discussion started by: pareshkp
4 Replies

3. Shell Programming and Scripting

awk --> math-operation in data-record and joining with second file data

Hi! I have a pretty complex job - at least for me! i have two csv-files with meassurement-data: fileA ...... (2 Replies)
Discussion started by: IMPe
2 Replies

4. UNIX for Dummies Questions & Answers

Des/awk for change format and adding integers in a column of data?

Greetings! I need a quick way to change the format in a table of data Here is an example of the input: 10 72 Value=177 VDB=0.0245 Value4=0,0,171,0 10 274 Value=238 VDB=0.0433 Value4=29,0,205,0 10 312 Value=222 VDB=0.0384 Value4=8,0,190,19 10 540 Value=405 VDB=0.0391 Value4=13,30,153,195... (3 Replies)
Discussion started by: Twinklefingers
3 Replies

5. Shell Programming and Scripting

Help with parsing data with awk , eliminating unwanted data

Experts , Below is the data: --- Physical volumes --- PV Name /dev/dsk/c1t2d0 VG Name /dev/vg00 PV Status available Allocatable yes VGDA 2 Cur LV 8 PE Size (Mbytes) 8 Total PE 4350 Free PE 2036 Allocated PE 2314 Stale PE 0 IO Timeout (Seconds) default --- Physical volumes ---... (5 Replies)
Discussion started by: rveri
5 Replies

6. Shell Programming and Scripting

Change data in file

Change File data Input A.txt LL725 LL725_A LL725 SIB.ra=6 LL725 LL725_B LL725 SIB.ra=11 LL726 LL726_A LL726 SIB.ra=20 LL726 LL726_B LL726 SIB.ra=6 LL726 LL726_C LL726 SIB.ra=10 Output :- LL725 LL725_A SIB.ra=6 LL725 LL725_B SIB.ra=11 LL726 LL726_A SIB.ra=20 (4 Replies)
Discussion started by: pareshkp
4 Replies

7. Shell Programming and Scripting

Change file content based on data

I have a Transaction File coming into the system. In this file, in all records the relevant data is as follows- Position 1:10 -> Transaction Code Position 252:255 -> 4 digit business code Now based on these 2 fields I have to alter value in Transaction code (Position 1:10)... (6 Replies)
Discussion started by: varunrbs
6 Replies

8. Programming

GNUPLOT- how to change the style of data points

Hi, I am trying to arrange my graphs with GNUPLOT. Although it looked like simple at the beginning, I could not figure out an answer for the following: I want to change the style of my data points (not the line, just exact data points) The terminal assigns first + and then x to them but what I... (0 Replies)
Discussion started by: natasha
0 Replies

9. Shell Programming and Scripting

loop through file to change some data

Hi, I have a file with the following data -0.00000 0.00000 0.00000 F F F 0.00000 0.00000 0.80000 F F F 0.50000 0.50000 0.60000 F F F 0.50000 0.50000 0.20000 F F F -0.00000 0.00000 0.40000 F F F I would like to change the last 3 lines from F F F to T T T. I tried looping each line but don't... (5 Replies)
Discussion started by: princessotes
5 Replies

10. Shell Programming and Scripting

How to change Raw data to Coloumn data fields

Dear All, I have some data file.see below. --------------ALARM CLEARING FROM SubNetwork=ONRM_RootMo,SubNetwork=AXE,ManagedElement=CGSN-------------- Alarm Record ID: 25196304 Event Time: 2006-08-28 13:41:35 Event Type: ... (1 Reply)
Discussion started by: Nayanajith
1 Replies
Login or Register to Ask a Question