awk : ignore first 5 line and last line of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk : ignore first 5 line and last line of a file
# 1  
Old 05-05-2011
awk : ignore first 5 line and last line of a file

Hi All,

I have text file like as below
Code:
 
temp.txt 1 Line 
temp.txt 2 Line 
temp.txt 3 Line 
temp.txt 4 Line 
temp.txt 5 Line 
temp.txt 6 Line 
temp.txt 7 Line 
temp.txt 8 Line 
temp.txt N Line

I expect the output like as below
Code:
 
processing 6 ...
processing 7 ...
processing 8 ...

I am using the below code to show the messge. But it process each and every line. How to ignore first 5 lines and Nth(Last) Line of the file. Please suggest some awk releated stuffs.
Code:
awk '{print "processing "$2 "\t..."}'

Thanks,
Mani Muthu
# 2  
Old 05-05-2011
Try this

Code:
awk 'NR > 5 {arr[i++]=$2} END{for(j=0;j<i-1;j++){print "processing "arr[j] "\t..."}}' file

regards,
Ahamed
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 05-05-2011
If field 2 in last line is N:
Code:
awk 'NR>5 && $2 !~ /^N$/ {printf("processing %s ...\n", $2)}' infile
processing 6 ...
processing 7 ...
processing 8 ...

If it doesn't matter/is dynamic what is in the last line:
Code:
awk -v nl=$(wc -l < infile) 'NR>5 && NR<nl {printf "processing %s ...\n", $2}'
processing 6 ...
processing 7 ...
processing 8 ...

Same but with awk only and an array:
Code:
awk 'NR>5 {_[z++]=$2} END{for(x=0; x<(z-1); x++){printf("processing %s ...\n", _[x])}}' infile
processing 6 ...
processing 7 ...
processing 8 ...

Another way, where it doesn't matter what the last line contains:
Code:
printf "processing %s ...\n" $(tail -n +6 infile| sed '$d'| cut -d" " -f2)
processing 6 ...
processing 7 ...
processing 8 ...

This User Gave Thanks to zaxxon For This Post:
# 4  
Old 05-05-2011
Hi ahamed101 & zaxxon,

Thanks for your solutions and its woking fine for me.

Thanks a lot
Mani Muthu
# 5  
Old 05-05-2011
Code:
tail -n +6 infile 
awk 'NR<6{next}{print "process line " NR}' infile
awk 'NR>5{print "process line " NR}' infile
sed '1,5d' infile

# 6  
Old 05-05-2011
Code:
awk 'NR > 6 { print "processing", x } { x = $2 }' infile

This User Gave Thanks to radoulov For This Post:
# 7  
Old 05-05-2011
ah ... ok, you wanted the $2 value ...

Code:
awk 'NR>5{x=$2;print "processing",x}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script using awk to find and replace a line, how to ignore comment lines

Hello, I have some code that works more or less. This is called by a make file to adjust some hard-coded definitions in the src code. The script generated some values by looking at some of the src files and then writes those values to specific locations in other files. The awk code is used to... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

2. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

3. UNIX for Dummies Questions & Answers

Ignore all lines except the --- dash line in a text file.

How do you write a script to ignore all lines except the --- dash lines and then remove --- dashes from the data in a text file? Also how do you separate data in a text file with a tab (for example, column1 (software) and column2 (date) ) ? Here is my scripts : I am getting errors in... (3 Replies)
Discussion started by: dellanicholson
3 Replies

4. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

5. Shell Programming and Scripting

Honey, I broke awk! (duplicate line removal in 30M line 3.7GB csv file)

I have a script that builds a database ~30 million lines, ~3.7 GB .cvs file. After multiple optimzations It takes about 62 min to bring in and parse all the files and used to take 10 min to remove duplicates until I was requested to add another column. I am using the highly optimized awk code: awk... (34 Replies)
Discussion started by: Michael Stora
34 Replies

6. Shell Programming and Scripting

[awk] line by line processing the same file

Hey, not too good at this, so I only managed a clumsy and SLOW solution to my problem that needs a drastic speed up. Any ideas how I write the following in awk only? Code is supposed to do... For every line read column values $6, $7, $8 and do a calculation with the same column values of every... (6 Replies)
Discussion started by: origamisven
6 Replies

7. Shell Programming and Scripting

how to compare file line by line with awk

im a newbee to unix. I have a requirement to compare two files with awk. file1.txt a b c d e file2.txt a b d e here i want to compare each line in file1 with corresponding line in file2 and prinf the line with difference. ie to check required result as shown below a=a (dont... (3 Replies)
Discussion started by: kiranps
3 Replies

8. Shell Programming and Scripting

awk concatenate every line of a file in a single line

I have several hundreds of tiny files which need to be concatenated into one single line and all those in a single file. Some files have several blank lines. Tried to use this script but failed on it. awk 'END { print r } r && !/^/ { print FILENAME, r; r = "" }{ r = r ? r $0 : $0 }' *.txt... (8 Replies)
Discussion started by: sdf
8 Replies

9. Shell Programming and Scripting

reading a file inside awk and processing line by line

Hi Sorry to multipost. I am opening the new thread because the earlier threads head was misleading to my current doubt. and i am stuck. list=`cat /u/Test/programs`; psg "ServTest" | awk -v listawk=$list '{ cmd_name=($5 ~ /^/)? $9:$8 for(pgmname in listawk) ... (6 Replies)
Discussion started by: Anteus
6 Replies

10. Shell Programming and Scripting

Awk not working due to missing new line character at last line of file

Hi, My awk program is failing. I figured out using command od -c filename that the last line of the file doesnt end with a new line character. Mine is an automated process because of this data is missing. How do i handle this? I want to append new line character at the end of last... (2 Replies)
Discussion started by: pinnacle
2 Replies
Login or Register to Ask a Question