Help with awk script and update position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with awk script and update position
# 1  
Old 02-15-2012
Help with awk script and update position

Hi, I'm using AIX 5.3 and am trying to get this to work as I need:

Code:
echo 6078:  6072  6073  6074  6075  6076  6077  6078  6079 | awk '{n=$8} n{sub(n,n+1000000)}1'
1006078: 6072 6073 6074 6075 6076 6077 6078 6079

I only want it to change second occurance of 6078 (ie $8) and not the first ($1).
I tried:

Code:
echo 6078:  6072  6073  6074  6075  6076  6077  6078  6079 | awk '{n=" "$8" "} n{sub(n," "n+1000000" ")}1'
6078: 6072 6073 6074 6075 6076 6077 1006078 6079

but I'm running piping through 10 awks (one to change each number), but on the rows with less than 10, it goes haywire if I have the " " in.
# 2  
Old 02-15-2012
Perl, ok?
Code:
echo 6078:  6072  6073  6074  6075  6076  6077  6078  6079 | perl -lane '$F[7]+=1000000;print join (" ",@F)'

# 3  
Old 02-15-2012
no, because as I said, I need to call it 10 times (as some lines have 10 entries, but some may have one). With the above, if I add
Code:
perl -lane '$F[9]+=1000000;print join (" ",@F)'

when there are only 8 entries, it outputs 1000000 as the last one instead of blank. Awk doesn't do that.
# 4  
Old 02-16-2012
Code:
echo 6075:  6072  6073  6074  6075  6076  6077  6078  6079 | \
perl -e 'chomp($x=<>);@x=split/\s+/,$x;for($i=1; $i<=$#x; $i++){if($x[0]=~/$x[$i]/){$x[$i]+=1000000;last}};print join(" ", @x);'

This User Gave Thanks to balajesuri For This Post:
# 5  
Old 02-16-2012
Try:
Code:
awk '{p=$1;for(i=2;i<=NF;i++)p=p OFS $i+1000000;print p}'

Code:
$ echo "6078:  6072  6073  6074  6075  6076  6077  6078  6079" | awk '{p=$1;for(i=2;i<=NF;i++)p=p OFS $i+1000000;print p}'
6078: 1006072 1006073 1006074 1006075 1006076 1006077 1006078 1006079

These 2 Users Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to retrieve values from XML file and update them in the same position! PLEASE HELP?

Good Day All Im quiet new to ksh scripting and need a bit of your help. I am attempting to write a script that reads in an XML and extracts certain field values from an XML file. The values are all alphanumeric and consist of two components: e.g "Test 1". I need to to create a script that... (2 Replies)
Discussion started by: JulioAmerica
2 Replies

2. Shell Programming and Scripting

Need command or script to print all lines from 2nd position to last but one position

hi guys, i want command or script to display the content of file from 2nd position to last but one position of a file abcdefghdasdasdsd 123,345,678,345,323 434,656,656,656,656 678,878,878,989,545 4565656667,65656 i want to display the same above file without first and... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

3. Shell Programming and Scripting

awk usage for position matching

i have a requirement like this if the line contains from position 294 to 299 is equal to "prabhu" ,then print entire line . i want to use awk awk '{if(substr(294-299) == 'prabhu') print "line" }' filename (1 Reply)
Discussion started by: ptappeta
1 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Find position of character with awk

Hi Guys! Could anyone help me with?.. I have a line which says BCVGF%6$#900 .....How can we know which position is for % or say $ by command or script?There is any way to get a prompt by any script? Thanks a lot (6 Replies)
Discussion started by: Indra2011
6 Replies

5. Shell Programming and Scripting

Awk Line/Row Position

Hi guys. I'd just like to know if its possible to change the actual line/row position in awk while its busy processing a file. In other words, is it possible to jump from line 10, back up to line 5 and continue processing line-by-line from then onwards? or is the way around this to add all lines... (3 Replies)
Discussion started by: going_grey
3 Replies

6. Shell Programming and Scripting

Position of a string using AWK

I'm plowing through a bunch of text (and I have to use awk) and need to identify which position a certain string is in. Consider the two lines... UP BROADCAST RUNNING SLAVE MULTICAST MTU:1372 Metric:1 UP LOOPBACK RUNNING MTU:16436 Metric:1 I can identify the line by searching for MTU:.... (3 Replies)
Discussion started by: scottwevans
3 Replies

7. Shell Programming and Scripting

Remove text from n position to n position sed/awk

I want to remove text from nth position to nth position couple of times in same line my line is "hello is there anyone can help me with this question" I need like this ello is there anyone can help me with question 'h' is removed and 'this' removed from the line. I want to do this... (5 Replies)
Discussion started by: elamurugu
5 Replies

8. Shell Programming and Scripting

AWK Position Variables

Hello guys, I'm stuck with AWK and probably stupid stuff. :rolleyes: I've a file with some values like: 123 456 10 11 90 39 20 ........... Then, extracting these vaues from that file is quite simple: tail -1 "filename"|awk '{printf $2}' This will show: 456 Now, what I don't realize is... (5 Replies)
Discussion started by: Lord Spectre
5 Replies

9. Shell Programming and Scripting

using awk removing newline and specific position

Hello Friends, Input File looks as follows: >FASTA Header1 line1 line2 line3 linen >FASTA Header2 Line1 Line2 linen >FASTA Header3 and so on ....... Output: Want something as: >FASTA Header1 line1line2line3linen >FASTA Header2 (5 Replies)
Discussion started by: Deep9000
5 Replies

10. Shell Programming and Scripting

awk script to update header record

I am using HP UX and think this may be done with awk but bot sure. I have a file with a several header records and undeneath many detail records I need to put in the header record the number of detail records above this header record and number of detail records below this header record Header... (5 Replies)
Discussion started by: klut
5 Replies
Login or Register to Ask a Question