awk - one liners


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - one liners
# 1  
Old 12-19-2012
awk - one liners

Guys,

I have a requirement like this.

A file has >5K records always. Separated by "|", it has 30 fields for each line. In some lines, I am getting an odd field. say, the 15th field is supposed to be 2 characters but comes in as >2. In this case, for resolving this I need to copy the value of 25th field to the 15th. See below.

Code:
||||||||||||||Praveen||||||||||PK||||||

Output needed
Code:
||||||||||||||PK||||||||||PK||||||

Lines tried:
Code:
awk FS="|" OFS="|" '{if(length($15)>2) $15=$25; print $0;}' file

Never succeeded! Please advise. Smilie
# 2  
Old 12-19-2012
Code:
 
$ cat input.txt
||||||||||||||Praveen||||||||||PK||||||
||||||||||||||Praveen||||||||||PK||||||
||||||||||||||PK||||||||||PK||||||
||||||||||||||PK||||||||||PK||||||
||||||||||||||Praveen||||||||||PK||||||
||||||||||||||Praveen||||||||||PK||||||
 
$ nawk -F\| -v OFS=\| 'length($15)>2{$15=$25}1' input.txt
||||||||||||||PK||||||||||PK||||||
||||||||||||||PK||||||||||PK||||||
||||||||||||||PK||||||||||PK||||||
||||||||||||||PK||||||||||PK||||||
||||||||||||||PK||||||||||PK||||||
||||||||||||||PK||||||||||PK||||||

your logic also work. explain the issue (what output you are getting with your awk? )
# 3  
Old 12-19-2012
Code:
$ cat file
||||||||||||||Praveen||||||||||PK||||||

$ awk 'BEGIN{FS=OFS="|"} length($15)>2{$15=$25}1' file
||||||||||||||PK||||||||||PK||||||

@Pikk45 - Just small change to your code.. also worksSmilie

Code:
 awk -F\| '{if(length($15)>2) $15=$25; print $0;}' OFS="|"  file


Last edited by pamu; 12-19-2012 at 05:52 AM..
# 4  
Old 12-19-2012
or
Code:
awk '{if(length($15)>2) $15=$25; print $0;}' FS="|" OFS="|"  file

awk 'length($15)>2{$15=$25}1' FS=\| OFS=\| file

# 5  
Old 12-19-2012
Thanks alot!

itkamaraj: nawk is not supported Smilie Smilie

pamu: Thanks man. You got me on this one!!! Smilie

Jotne: Thanks a bunch Smilie Smilie I should have learnt about adding FS n OFS to the end Smilie
# 6  
Old 12-19-2012
try awk instead of nawk.
# 7  
Old 12-19-2012
Code:
awk '{sub(/...+/,$25,$15)}1' FS=\| OFS=\| file

Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. What is on Your Mind?

One liners, quick rant...

Hi fellas and fella-esses... There are numerous threads on here where people ask for, and often expect, solutions to difficult problems solved using _one_liners_. Why do they insist as such when it is virtually no different in execution time with well thought out indented code? Is it me... (12 Replies)
Discussion started by: wisecracker
12 Replies

2. Shell Programming and Scripting

awk one liners into a nice script

Hi All, I got some awk one liners, how can i split it all into a nice script? Got these: gzcat capgw0.log-201308161376632741.gz | sed -n '/2013-08-16 05:56:/,/2013-08-16 05:58:/p' > timebased.log awk -F":|," 'FNR==NR && /INFO - AId:/ {a=$0;next} END {for (i in a) print i "|" a}'... (8 Replies)
Discussion started by: batka
8 Replies

3. What is on Your Mind?

Those simple one liners

I wanted to say LOL and punch my face when I saw post#11 (where Don_Cragun even reduced the string manipulation with a simple regex) in the thread https://www.unix.com/shell-programming-scripting/220553-add-0-start-filename-2.html I mean, when things can be done with just a one liner, sometimes I... (6 Replies)
Discussion started by: ahamed101
6 Replies

4. Shell Programming and Scripting

invaluable resource for shell one liners and more

Hi all, This link might help whom is seeking a quick solutions for thier daily shell needs. linkAll commands | commandlinefu.com (1 Reply)
Discussion started by: h@foorsa.biz
1 Replies
Login or Register to Ask a Question