To search a file for a specific word in a file using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To search a file for a specific word in a file using shell script
# 1  
Old 09-01-2009
To search a file for a specific word in a file using shell script

Hi All,
I have a sql output file has below. I want to get the values 200000040 and 1055.49 .Can anyone help me to write a shell script to get this.


ACCOUNT_NO
------------------------------------------------------------
BILL_NO PAY_TYPE
------------------------------------------------------------ ----------
AMT_NOT_BILLED CASH_ON_DELIVERY
-------------- ----------------
COD_BILL_NO IS_SUSPENDED
------------------------------------------------------------ ------------
200000040
B1-57 10005
1055.49 0
0


Thanks in Advance.
Girish.
# 2  
Old 09-01-2009
try..
Code:
-bash-3.2$ cat list2
ACCOUNT_NO
------------------------------------------------------------
BILL_NO PAY_TYPE
------------------------------------------------------------ ----------
AMT_NOT_BILLED CASH_ON_DELIVERY
-------------- ----------------
COD_BILL_NO IS_SUSPENDED
------------------------------------------------------------ ------------
200000040
B1-57 10005
1055.49 0
0
-bash-3.2$
-bash-3.2$ sed -n '9 p' list2
200000040
-bash-3.2$ sed -n '11 p' list2
1055.49 0

or

Code:
-bash-3.2$ sed '9 !d' list2
200000040
-bash-3.2$ sed '11 !d' list2
1055.49 0
-bash-3.2$

# 3  
Old 09-01-2009
Try this one

cat inputfile | awk -F " " '/^[[:digit:]]/ { print $1 }'
# 4  
Old 09-01-2009
Hi ryandegrate25,
Thanks for the reply. Your command works for my requirment.
Actually i have similar kind of records in a list2, in this case how will i use your command. I tried using it but its failing for multiple records of same type(which i posted earlier).

Thanks in advance, Giri
# 5  
Old 09-01-2009
the number in
Code:
sed '11 !d' list2

refers to line number... if your input has a fixed line numbers then you can use it else you can use pattern searching like posted by oky
# 6  
Old 09-01-2009
Hi oky,
In your commane "cat inputfile | awk -F " " '/^[[:digit:]]/ { print $1 }' "

Can i know what does "digit" refers to.?
Actually the input file format which i had posted will have similar kind mulitple records in a file.Considering this can you help me to get a command.

Also now i tried using your above command on multiple records, it gives me only 200000040 and not the 1055.49.. Please help.

Thanks in advance.
Giri

Last edited by girish.raos; 09-01-2009 at 08:06 AM..
# 7  
Old 09-01-2009
oky's solution will apply to the whole file.
it will do the same whenever the condition matches.

to remove the last line i.e "0" you can do something like below:
Code:
awk -F " " '{ if ($0 ~ /^[[:digit:]]/ && $0 != 0) { print $1 }}' inputfile


inputfile:

Code:
ACCOUNT_NO
------------------------------------------------------------
BILL_NO PAY_TYPE
------------------------------------------------------------ ----------
AMT_NOT_BILLED CASH_ON_DELIVERY
-------------- ----------------
COD_BILL_NO IS_SUSPENDED
------------------------------------------------------------ ------------
200000040
B1-57 10005
1055.49 0
0
 
ACCOUNT_NO
------------------------------------------------------------
BILL_NO PAY_TYPE
------------------------------------------------------------ ----------
AMT_NOT_BILLED CASH_ON_DELIVERY
-------------- ----------------
COD_BILL_NO IS_SUSPENDED
------------------------------------------------------------ ------------
666666040
B1-57 10005
1088.49 0
0
 
ACCOUNT_NO
------------------------------------------------------------
BILL_NO PAY_TYPE
------------------------------------------------------------ ----------
AMT_NOT_BILLED CASH_ON_DELIVERY
-------------- ----------------
COD_BILL_NO IS_SUSPENDED
------------------------------------------------------------ ------------
222220040
B1-57 10005
3355.49 0
0

output:
Code:
200000040
1055.49
666666040
1088.49
222220040
3355.49

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

2. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

3. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word "description" excluding weird characters like $&lmp and without html tags in the new file output.txt. Help me. Thanx in advance. I have attached the input... (4 Replies)
Discussion started by: sachit adhikari
4 Replies

4. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script?

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word description excluding weird characters like $$#$#@$#@***$# and without html tags in the new file output.txt. Help me. Thanx in advance. My final goal is to... (11 Replies)
Discussion started by: sachit adhikari
11 Replies

5. UNIX for Dummies Questions & Answers

Shell script find word from one file and insert in another file

Hi, I am new to shell scripting. I need a bash shell scripts which search and grep a parameter value from input.txt file and insert it in between two semicolon of second line of output.txt file. For example The shell script search an IP address as parameter value from input.txt ... (2 Replies)
Discussion started by: sunilkumarsinha
2 Replies

6. UNIX for Dummies Questions & Answers

Search a specific word from any one of the file.

how do i Search a specific word from any one of the file.? (1 Reply)
Discussion started by: ritusubash
1 Replies

7. UNIX for Dummies Questions & Answers

Shell script to search for text in a file and copy file

Compete noob question.... I need a script to search through a directory and find files containing text string abcde1234 for example and then copy that file with that text string to another directory help please :eek: (9 Replies)
Discussion started by: imeadows
9 Replies

8. UNIX for Dummies Questions & Answers

How to count the occurences of a specific word in a file in bash shell

Hello, I want to count the occurences of a specific word in a .txt file in bash shell. Can somebody help me pleaze?? Thanks!!! (2 Replies)
Discussion started by: mskart
2 Replies

9. Shell Programming and Scripting

search for the contents in many file and print that file using shell script

hello have a file1 H87I Y788O T347U J23U and file2 J23U U887Y I99U T556U file3 I99O J99T F557J file4 N99I T666U R55Y file5 H87I T347U file6 H77U R556Y E44T file7 Y788O K98U H8I May be using script we can use file1 to search for all the files and have the output H87I file5... (3 Replies)
Discussion started by: cdfd123
3 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question