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
# 1  
Old 03-14-2011
awk print last line returns empty string

hello

I have a file with lines of info separated with "|"

I want to amend the second field of the last line, using AWK
my problem is with geting awk to return the last line

this is what I am using

Code:
 
awk  'END{ print  $0 }' myFile

but I get an empty result

I tried the same thing with tail
Code:
 
tail -1 myFile

which works fine and returns the last line, however I need it to work with awk

Also tried print, print $1, print $* with no luck,

any suggestion would be much appriciated!

---------- Post updated at 03:59 PM ---------- Previous update was at 03:53 PM ----------

This is what I get

Code:
 
$ tail -1 myFile 
251|21|FOOTER|FILE||22
$ awk 'END{print}' myFile
 
$

# 2  
Old 03-14-2011
Code:
awk '{x=$0}END{print x}' myFile

or
Code:
sed '$!d' myFile

or
Code:
sed -n '$p' myFile

or
Code:
tail -1 myFile

# 3  
Old 03-14-2011
Quote:
Originally Posted by ctsgnb
Code:
awk '{x=$0}END{print x}' myFile

I think it should be written like below:

Code:
awk -F"|" '{x=$0}END{print x}' myFile

# 4  
Old 03-14-2011
Quote:
Originally Posted by ahmad.diab
I think it should be written like below:

Code:
awk -F"|" '{x=$0}END{print x}' myFile

Ooops ... i was focusing on getting the last line Smilie

Code:
nawk -F\| '{$2="something_else_modified";x=$0}END{print x}' OFS=\| infile


Last edited by ctsgnb; 03-14-2011 at 01:41 PM..
# 5  
Old 03-14-2011
Sadly the awk way still returns empty
so i tried this:

Code:
 
var1=$(cat myFile | wc -l)
echo $var1
 
awk -F"|" '$1==var1 {$2=0; print >> "myFile"}' OFS="|" myFile

now I know that I cannot just use $ to pass shell variables into awk, and the above does not work, how can I pass var1 into the awk?
ideally this what I want to do, "on the last line, change the second field to 0"
# 6  
Old 03-14-2011
Code:
awk -F\| 'END{print $2}' file

Smilie

---------- Post updated at 12:43 PM ---------- Previous update was at 12:41 PM ----------

Code:
var=VAR
awk -F\| -vv=$var 'END{print $2, v}' file

# 7  
Old 03-14-2011
Quote:
Originally Posted by danmero
Code:
awk -F\| 'END{print $2}' file

Smilie

---------- Post updated at 12:43 PM ---------- Previous update was at 12:41 PM ----------

Code:
var=VAR
awk -F\| -vv=$var 'END{print $2, v}' file


you got me very confused here...

this what I tried after your reply:

Code:
 
var1=$(cat myFile| wc -l)
echo $var1
 
awk -F"|" -v v="$var1" ' $1=v {$2=0; print >> "myFile"}' myFile

I dont just want to print the second field of the last line, I want to change it to 0, my problem is END does not work as for some reason it thinks the last line is empty

now what it does is changing everything to 0 infinetly...

Last edited by TasosARISFC; 03-14-2011 at 02:16 PM.. Reason: more info
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