awk, extract last line of multiple files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk, extract last line of multiple files
# 1  
Old 10-21-2010
awk, extract last line of multiple files

Hi,
I have a directory full of *.txt files. I would like to print the last line of every file to screen.
I know you can use FNR for printing the first line of each file, but how do I access the last line of each file?

This code doesn't work, it only prints the last line of the last file:
Code:
BEGIN {
  print "Starting script...";
}
{
  last_line = $0;
} 
END {
  printf "\n" last_line;
  printf "\nEnding script...\n";
}

Note: I prefer coding by creating a file script.awk and running it using awk -f script.awk *.txt
ThanksSmilie
# 2  
Old 10-21-2010
Code:
tail -1 file*

# 3  
Old 10-21-2010
Code:
tail: option used in invalid context -- 1

# 4  
Old 10-21-2010
Quote:
Originally Posted by Scrutinizer
Code:
tail: option used in invalid context -- 1

I get the message "tail:: Too many arguments.". Should I be replacing text in your example with text relevant to my example? I'm not familiar with a lot of the syntax.
# 5  
Old 10-21-2010
You could use tail like this:
Code:
for i in *.txt
do 
  tail -1 "$i"
done

Your script can be adjusted like this
Code:
BEGIN {
  print "Starting script...";
}
  last_file != FILENAME && NR!=FNR {
  print last_line
}
{ last_line = $0
  last_file = FILENAME
}

END {
  print last_line;
  printf "\nEnding script...\n"
}

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 10-21-2010
Quote:
Originally Posted by Scrutinizer
You could use tail like this:
Code:
for i in *.txt
do 
  tail -1 "$i"
done

Your script can be adjusted like this
Code:
BEGIN {
  print "Starting script...";
}
  last_file != FILENAME && NR!=FNR {
  print last_line
}
{ last_line = $0
  last_file = FILENAME
}
 
END {
  print last_line;
  printf "\nEnding script...\n"
}

That works exactly like I want....fantastic! ThanksSmilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - Multiple files - 1 file with multi-line data

Greetings experts, Have 2 input files, of which 1 file has 1 record per line; in 2nd file, multiple lines constitute 1 record; Hence declared the RS=";" Now in the first file which ends with ";" at each line of the line; But \nis also being considered as part of the data due to which I am... (1 Reply)
Discussion started by: chill3chee
1 Replies

2. Shell Programming and Scripting

Extract multiple strings from line

Hello I have an output that has a string between quotes and another between square brackets on the same line. I need to extract these 2 strings Example line Device "nrst3a" attributes=(0x4) RAW SERIAL_NUMBER=SNL2 Output should look like nrst3a VD073AV1443BVW00083 I was trying with sed... (3 Replies)
Discussion started by: bombcan
3 Replies

3. Shell Programming and Scripting

Compare multiple files, and extract items that are common to ALL files only

I have this code awk 'NR==FNR{a=$1;next} a' file1 file2 which does what I need it to do, but for only two files. I want to make it so that I can have multiple files (for example 30) and the code will return only the items that are in every single one of those files and ignore the ones... (7 Replies)
Discussion started by: castrojc
7 Replies

4. UNIX for Dummies Questions & Answers

Using AWK: Extract data from multiple files and output to multiple new files

Hi, I'd like to process multiple files. For example: file1.txt file2.txt file3.txt Each file contains several lines of data. I want to extract a piece of data and output it to a new file. file1.txt ----> newfile1.txt file2.txt ----> newfile2.txt file3.txt ----> newfile3.txt Here is... (3 Replies)
Discussion started by: Liverpaul09
3 Replies

5. UNIX for Dummies Questions & Answers

AWK, extract data from multiple files

Hi, I'm using AWK to try to extract data from multiple files (*.txt). The script should look for a flag that occurs at a specific position in each file and it should return the data to the right of that flag. I should end up with one line for each file, each containing 3 columns:... (8 Replies)
Discussion started by: Liverpaul09
8 Replies

6. Shell Programming and Scripting

Split line to multiple files Awk/Sed/Shell Script help

Hi, I need help to split lines from a file into multiple files. my input look like this: 13 23 45 45 6 7 33 44 55 66 7 13 34 5 6 7 87 45 7 8 8 9 13 44 55 66 77 8 44 66 88 99 6 I want to split every 3 lines from this file to be written to individual files. (3 Replies)
Discussion started by: saint2006
3 Replies

7. Shell Programming and Scripting

How to extract multiple line in a paragraph? Please help.

Hi all, The following lines are taken from a long paragraph: Labels of output orbitals: RY* RY* RY* RY* RY* RY* 1\1\GINC-COMPUTE-1-3\SP\UB3LYP\6-31G\C2H5Cr1O1(1+,5)\LIUZHEN\19-Jan-20 10\0\\# ub3lyp/6-31G pop=(nbo,savenbo) gfprint\\E101GECP\\1,5\O,0,-1.7 ... (1 Reply)
Discussion started by: liuzhencc
1 Replies

8. Shell Programming and Scripting

need help with post:extract multiple columns from multiple files

hello, I will would be grateful if anyone can help me reply to my post extract multiple cloumns from multiple files; skip rows and include filenames; awk Please see this thread. Thanks manishabh (0 Replies)
Discussion started by: manishabh
0 Replies

9. Shell Programming and Scripting

extract multiple cloumns from multiple files; skip rows and include filenames; awk

Hello, I am trying to write a bash shell script that does the following: 1.Finds all *.txt files within my directory of interest 2. reads each of the files (25 files) one by one (tab-delimited format and have the same data format) 3. skips the first 10 rows of the file 4. extracts and... (4 Replies)
Discussion started by: manishabh
4 Replies

10. Shell Programming and Scripting

how to extract multiple strings from a line

Hi I have the following requirement. i have the following line from a log file one : two : Three : four : five : six : seven : eight :nine :ten Now can you pls help what i should do to get only the following output from the above line two : five : six : seven : Eight appreciate your... (3 Replies)
Discussion started by: vin_eme
3 Replies
Login or Register to Ask a Question