awk print last line returns empty string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk print last line returns empty string
# 8  
Old 03-14-2011
Code:
/usr/xpg4/bin/awk -F\| '{$2=0;x=$0} END {print x}' OFS=\| myFile

If you are on solaris, use nawk instead of awk or use the posix awk which should be located in /usr/xpg4/bin/awk

Code:
# tail -1 infile
"CCCC"|"My Test C"|"Test "CCCC""|"This is C"
# r=REPLACE_STRING
# nawk -F\| -v V="$r" '{$2=V;x=$0}END{print x}' OFS=\| infile
"CCCC"|REPLACE_STRING|"Test "CCCC""|"This is C"
#


Last edited by ctsgnb; 03-14-2011 at 02:16 PM..
# 9  
Old 03-14-2011
sadly I am not on solaris...


I am bumbed now, it keeps changing everything to 0 now...

This should be very simple, just go to the last line, change the second field to 0 save file, end of story, no weird sed, no solaris,

var1=$(cat SocaFileFixed_Final | wc -l)
echo $var1

awk -F"|" -v v="$var1" ' $1==v {$2=0}' myfile

can't see why this doesn't work SmilieSmilieSmilie
# 10  
Old 03-14-2011
Hey u are comparing $1 with the nof lines in the file. Does your $1 contain line numbers?
# 11  
Old 03-14-2011
Code:
nawk 'FNR==1{p=$0;next}{print p;p=$0}END {$0=p;$2=0;print}' myFile

# 12  
Old 03-14-2011
Better try this one
var=$(cat filename|wc -l)
awk -F"|" -v var=$var ' NR==var {$2=0; print $0}'
# 13  
Old 03-14-2011
Ok found it
heres the code that works as I want

Code:
 
rm temp3
var1=$(cat myFile | wc -l)
echo $var1
 
awk -F"|" -v v="$var1" -v  c=0 ' $1==v{$2=0;print >>"temp3" ; prev=c ; c=c+1 ;next} {print >> "temp3"}' OFS="|" myFile

# 14  
Old 03-14-2011
Code:
sed -i '$s:[^|]*|:0|:2' infile

Code:
 # cat infile
"AAAA"|"Test "1-A""|"Test AAAA"|"This is A"
"BBBB"|"Test "1-B""|"Test BBBB"|"This is B"
"CCCC"|"My Test C"|"Test "CCCC""|"This is C"

Code:
# sed '$s:[^|]*|:0|:2' infile
"AAAA"|"Test "1-A""|"Test AAAA"|"This is A"
"BBBB"|"Test "1-B""|"Test BBBB"|"This is B"
"CCCC"|0|"Test "CCCC""|"This is C"


Which OS + version do you run ?

Last edited by ctsgnb; 03-14-2011 at 02:47 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print the string between 3rd and 4th backslashs to end of line

im trying to get awk to print the string between 3rd and 4th backslashs to end of line test could be any word this http://example.com/test/ >to this http://example.com/test/ > testalso the other way round insert string at end of line... (13 Replies)
Discussion started by: bob123
13 Replies

2. Shell Programming and Scripting

awk to identify empty fields in line

I am trying to use awk to identify and print out records in fields that are empty along with which line they are in. I hope the awk below is close, it runs but nothing results. Thank you :). awk awk -F'\t' 'FNR==NR ~ /^*$/ { print "NR is empty" }' file file 123 GOOD ID 45... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Programming

[SOLVED] C++ print next line until line.empty

Hi, could you please help with the following: I have an input file like: one two three four five six I want to print the lines starting from 'three' to the empty line. Something like that: if ( line == "three" ) { while ( !line.empty() ) { cout <<... (0 Replies)
Discussion started by: apenkov
0 Replies

4. Shell Programming and Scripting

Python call to bash script returns empty string

Hi all, I need help figuring out why my script won't work when envoked from web interface. First off, I'm fairly new to Linux and Shell scripting so, my apologies for being ignorant! So I have a python script that I envoke through a web interface. This script envokes my shell script and... (8 Replies)
Discussion started by: arod291
8 Replies

5. Shell Programming and Scripting

Shell Variable in Curly Brackets Returns Empty Value

Hello Team, I have a script which will grep for a time from a file. I have following code to grep for a time in a file. node=`hostname` current_date=`date` file11=weblogic.log next_date=`date '+%b %e, %Y'` next_date_time11=`grep -i "${#next_date}" ${file11}| tail -1 | awk... (3 Replies)
Discussion started by: coolguyamy
3 Replies

6. Shell Programming and Scripting

awk script creates empty line

I have the following awk script to which I pass the file > 10 0 0 10 0 0 > 12.997 0 5.71132 12.9098 0.0687626 5.48855 12.7506 0.174324 5.13225 12.5913 0.262662 4.80643 12.4316 0.335652 4.50283 12.2717 0.394598 4.21542 12.1113 0.440399 3.93957 11.9506 0.473646 3.67148 11.7894... (2 Replies)
Discussion started by: kristinu
2 Replies

7. Shell Programming and Scripting

awk find a string, print the line 2 lines below it

I am parsing a nagios config, searching for a string, and then printing the line 2 lines later (the "members" string). Here's the data: define hostgroup{ hostgroup_name chat-dev alias chat-dev members thisisahostname } define hostgroup{ ... (1 Reply)
Discussion started by: mglenney
1 Replies

8. Shell Programming and Scripting

awk print second line after search string

I have multiple config files where I need to pull the ip address from loopback3. The format is the same in every file, the ip is the second line after interface loopback3. interface loopback2 loopback description router ID ip address 192.168.1.1 interface loopback3 loopback description... (3 Replies)
Discussion started by: numele
3 Replies

9. Shell Programming and Scripting

awk to print lines based on string match on another line and condition

Hi folks, I have a text file that I need to parse, and I cant figure it out. The source is a report breaking down softwares from various companies with some basic info about them (see source snippet below). Ultimately what I want is an excel sheet with only Adobe and Microsoft software name and... (5 Replies)
Discussion started by: rowie718
5 Replies

10. Shell Programming and Scripting

How to print empty line when the a field is changed

Hi all, I have this input file .. BSS01 107 Swafieh 11/06/2008 12:06:57 BSS01 111 Ramada_Hotel 12/06/2008 11:37:20 BSS01 147 Kalha_Rep 11/06/2008 19:13:39 BSS01 147 Kalha_Rep ... (9 Replies)
Discussion started by: yahyaaa
9 Replies
Login or Register to Ask a Question