Print line number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print line number
# 1  
Old 11-30-2011
Print line number

how i need help again

i have file input.txt like this
Code:
48:20111008_20111122:31231|123|
78:20111008_20111122:435453|321112|

where 48 and 78 is line number 20111008_20111122 is a file and 31231|123| i want get in file, i am using manual awk for get that file like this
Code:
awk 'NR==48 {print;exit}' 20111008_20111122 >> filer
awk 'NR==78 {print;exit}' 20111008_20111122 >> filer

now what should i do if input.txt can looping using the awkSmilie please helpme

thx for your advice
# 2  
Old 11-30-2011
Just to print 2nd, 4th and 8th line using loop ..
Code:
$ for i in 2 4 8 ; do nawk -v lno="$i" 'NR==lno {print;exit}' infile; done


Last edited by jayan_jay; 11-30-2011 at 05:25 AM.. Reason: code corrected
# 3  
Old 11-30-2011
where 48 and 78 is line number 20111008_20111122 is a file and 31231|123| i want get in file

i dont understand the above highlighted one. Can you provide some sample input files and outputfiles.
# 4  
Old 11-30-2011
what is your desired output?
# 5  
Old 11-30-2011
Code:
while read line
do
 lno=$(echo $line | cut -d: -f1)
 fname=$(echo $line | cut -d: -f2)
 nawk -v lno="$lno" 'NR==lno{print;exit}' ${fname} >> filer
done < input.txt

This User Gave Thanks to itkamaraj For This Post:
# 6  
Old 11-30-2011
Quote:
Originally Posted by jayan_jay
Try with this in loop ..
Code:
$ line=5
$ nawk -v lno="$line" 'NR==lno {print;exit}' infile

hlow @jayan
thx for your respon

bu its same with awk
Code:
awk 'NR==48 {print;exit}' 20111008_20111122 >> filer 
awk 'NR==78 {print;exit}' 20111008_20111122 >> filer

i just want to create shell
for this file
Code:
48:20111008_20111122:31231|123| 
78:20111008_20111122:435453|321112|

can do autolooping using awk like above meybe i think using for i or while
but i can't for substitution this variableSmilie

---------- Post updated at 04:37 AM ---------- Previous update was at 04:28 AM ----------

Quote:
Originally Posted by itkamaraj
where 48 and 78 is line number 20111008_20111122 is a file and 31231|123| i want get in file

i dont understand the above highlighted one. Can you provide some sample input files and outputfiles.
its index i create for get 31231|123| in big file so i must grep that with line number and the master(20111008_20111122) file

---------- Post updated at 04:42 AM ---------- Previous update was at 04:37 AM ----------

Quote:
Originally Posted by itkamaraj
Code:
while read line
do
 lno=$(echo $line | cut -d: -f1)
 fname=$(echo $line | cut -d: -f2)
 nawk -v lno="$lno" 'NR==lno{print;exit}' ${fname} >> filer
done < input.txt

that's i need thx @itkamaraj and @jayan_jay
# 7  
Old 11-30-2011
you can try this
Code:
# awk -F':' '{system("awk NR=="$1"{print} " $2)}' infile.txt >> filer

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print line number from piped output

i need to do something like this: script.sh #!/bin/sh echo "hello" echo "My First name is John" echo "My Last name is Smith" echo "I am here to save you a lot of work" sed -n 4,5p $0 i dont want to run the script. i just want to pull out specific line from it. so the logic here... (5 Replies)
Discussion started by: SkySmart
5 Replies

2. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

3. UNIX for Dummies Questions & Answers

Print enter line number

Hi, I have below text file wer,r5yut,dfy5y,dytry w45w,vbjjbh,xfsd,ctfyuk xdte56,rty6r,r645,976fd w34r,dtyer,d5tyty,fdyrt I want to get an O/P the enter rows for example if I enter 2, the first row should print output wer,r5yut,dfy5y,dytry w45w,vbjjbh,xfsd,ctfyuk (4 Replies)
Discussion started by: stew
4 Replies

4. Shell Programming and Scripting

print lines between line number

Hi, Anyone help me to print the lines from the flat file between 879th line number and 1424th line number. The 879 and 1424 should be passed as input to the shell script(It should be dynamic). Can any one give me using sed or awk? I tried using read, and print the lines..Its taking too... (3 Replies)
Discussion started by: senthil_is
3 Replies

5. Shell Programming and Scripting

print the number of fields in each line

I am looking for equivalent of following awk command in perl # awk '{ print NF ":" $0 } ' junk1 8:VAH NIC_TYPE CONFIG SIZE_GB PILO KOM BHA_GRP DESCR 8:2 NIC6 cont 34 y n shal_orgrp /shal 8:4 NIC5 signa 52 n y shal_orgrp... (3 Replies)
Discussion started by: dynamax
3 Replies

6. Shell Programming and Scripting

print number of words in each line

Hi, Please suggest a way to print number of words in the end of each line. <input file> red aunt house blue sky bat and ball game <output file> red aunt house 3 blue sky 2 bat and ball game 4 Thanks! (2 Replies)
Discussion started by: mira
2 Replies

7. Shell Programming and Scripting

sed script - print the line number along with the line

Hi, I want to print the line number with the pattern of the line on a same line using multi-patterns in sed. But i don't know how to do it. For example, I have a file abc def ghi I want to print 1 abc 2 def 3 ghi I know how to write it one line code, but i don't know how to put... (11 Replies)
Discussion started by: ntpntp
11 Replies

8. Shell Programming and Scripting

Print selection of line based on line number

Hi Unix gurus Basically i am searching for the pattern and getting the line numbers of the grepped pattern. I am trying to print the series of lines from 7 lines before the grepped line number to the grepped line number. I am trying to use the following code. but it is not working. cat... (3 Replies)
Discussion started by: mohanm
3 Replies

9. Shell Programming and Scripting

Regarding about the print line number/order

Hello, I am trying to print line number/order using this command awk '{print $0, FNR}' myfilename 11006 A41 1888 11006 A41 1888 11006 A41 1888 11006 A41 ... (2 Replies)
Discussion started by: davidkhan
2 Replies

10. Shell Programming and Scripting

print first number in each line

I have a file such as: .....12345......67890...xxx ....123456....78901...yyy ...1234567...89012...zzz ..12345678.90123...aaa Where the '.' character is a SPACE. I'm trying to print just the first number in each line. such as: 12345 123456 1234567 12345678 Both the number of... (1 Reply)
Discussion started by: dcfargo
1 Replies
Login or Register to Ask a Question