[ask]filter with line number in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [ask]filter with line number in file
# 1  
Old 06-15-2011
[ask]filter with line number in file

dear all
about my last question

in fileA.txt (all list line i want to find in fileB.txt)
is content of line number
fileA.txt
23
34
35
55
59

and fileB.txt is content like
24:asd|ekkk|001|001
123:bca|egsd|210|002
1231:cas|egds|322|231
...
in fileB.txt they have line number like example:
line number 24 is asd|ekkk|001|001
line number 123 is bca|egsd|210|002

now how if i have line number in fileA.txt and
make me faster find line in fileB.txt so not scan all line number and to the point to target....Smilie

thx for help me
# 2  
Old 06-15-2011
You can do something like that :
Code:
$ cat fileA.txt
23
34
35
55
59
$ cat fileB.txt
23:klm|azer|000|601
24:asd|ekkk|001|001
123:bca|egsd|210|002
1231:cas|egds|322|231
35:xyz|abc|999|224
1:mno|def|333|097
$ awk -F: 'NR==FNR {L[$1]++; next;} $1 in L' fileA.txt fileB.txt
23:klm|azer|000|601
35:xyz|abc|999|224
$

Jean-Pierre.
This User Gave Thanks to aigles For This Post:
# 3  
Old 06-15-2011
The ++ could even be omitted (useless) :

Code:
nawk -F: 'NR==FNR {L[$1];next}$1 in L' file1 file2

This User Gave Thanks to ctsgnb For This Post:
# 4  
Old 06-15-2011
thx all you are helping my job.....
# 5  
Old 06-19-2011
one i think how about using sed...like this

sed -n -e {fileA.txt}p fileB.txt

to get line in fileB.txt

thx become
# 6  
Old 06-20-2011
One more way ..

cat fileA.txt | xargs | sed 's, ,|,g;s,^,egrep ",g;s,$,\" fileB.txt,g' | sh

---------- Post updated at 05:56 PM ---------- Previous update was at 05:51 PM ----------

slight modification..

cat fileA.txt | xargs | sed 's,^,\^,g;s, ,|^,g;s,^,egrep ",g;s,$,\" fileB.txt,g' | sh

Output:
---------
23:klm|azer|000|601
35:xyz|abc|999|224
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Hos to get the line number of uncommented line from the file

I have few lines in a text file. I am trying to get the line number of uncommented line from the text file using unix shell script. For example : I want the line number of Orange from the below text file. Here expected answer is 4 since the line 2 is commented. Apple #Orange grapes Orange (4 Replies)
Discussion started by: Brennon
4 Replies

2. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

Replace first number of each line in a file with another number

Hi, I have a set of files in a directory that I have to read and replace the first occurrence of a number with another dummy number. This is what I have so far but it does not seem to work. The files have lot of other data in each row and each data element is separated by ,@, for file in... (13 Replies)
Discussion started by: scorpioraghu
13 Replies

4. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

5. Shell Programming and Scripting

Filter files by exact line anywhere in file

I have two files. L1 and L2. (each file, has one string per line without any spaces) I would like to get those lines in L1 that are not present anywhere in L2. No substring match, just exact line by line match but it can be anywhere in the file. (line 5 in L1 may be present in line 22 of L2,... (2 Replies)
Discussion started by: nick2011
2 Replies

6. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

7. Shell Programming and Scripting

How to filter only comments while reading a file including line break characters.

How do I filter only comments and still keep Line breaks at the end of the line!? This is one of the common tasks we all do,, How can we do this in a right way..!? I try to ignore empty lines and commented lines using following approach. test.sh # \040 --> SPACE character octal... (17 Replies)
Discussion started by: kchinnam
17 Replies

8. Shell Programming and Scripting

how to get the data from line number 1 to line number 100 of a file

Hi Everybody, I am trying to write a script that will get some perticuler data from a file and redirect to a file. My Question is, I have a Very huge file,In that file I have my required data is started from 25th line and it will ends in 100th line. I know the line numbers, I need to get all... (9 Replies)
Discussion started by: Anji
9 Replies

9. UNIX for Dummies Questions & Answers

how do I filter for a specific line in a file?

It seems like it should be very simple to have a command like grep return a specific line from a file but I can't find anything in the man pages to make grep search by line. I've looked in cat as well. Anyone know how to return a single line from a file? (5 Replies)
Discussion started by: gelitini
5 Replies

10. Shell Programming and Scripting

Appending the line number and a seperator to each line of a file ?

Hi, I am a newb as far as shell scripting and SED goes so bear with me on this one. I want to basically append to each line in a file a delimiter character and the line's line number e.g Change the file from :- aaaaaa bbbbbb cccccc to:- aaaaaa;1 bbbbbb;2 cccccc;3 I have worked... (4 Replies)
Discussion started by: pjcwhite
4 Replies
Login or Register to Ask a Question