Using first word and print their contents using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using first word and print their contents using awk
# 1  
Old 10-09-2007
Using first word and print their contents using awk

suppose u hava a file
G1354R tGGC-CGC
D1361N cGAC-AAC
I1424T ATC-ACC
R768W gCGG-TGG
Q1382R CAG-CGG
Q178E gCAG-GAG
Y181C TAC-TGC
.........cont.
So the question is
By searching for first word i.e.character say R
output shud be
R768W gCGG-TGG
R182P CGG-CCG
R189W gCGG-TG
if Q then
Q1382R CAG-CGG
Q178E gCAG-GAG
So.... continue with all characters .i.e. first word ..

Thanks
# 2  
Old 10-09-2007
try this out,

awk '/^Q/ {print $0}' <<file name>>

Q1382R CAG-CGG
Q178E gCAG-GAG
# 3  
Old 10-09-2007
Code:
grep '^Q' filename

# 4  
Old 10-09-2007
What about if you need to extract the lines based on 5th charater in first word.
# 5  
Old 10-09-2007
do this-

awk 'BEGIN{FS=" "}{if ( substr($1,5,1) == "Q" ) print $0}' file


cheers,
Devaraj Takhellambam
# 6  
Old 10-09-2007
Or:
Code:
grep ^....Q filename

With awk it would be:

Code:
awk '/^....Q/' filename

# 7  
Old 10-10-2007
shell sed

Hi,
This one should be ok.

Code:
a=1;
while [ $a -le 1 ]
do
	echo "please input the character you would like to search,XX for exit"
	read var
	if [ $var = "XX" ]
	then
		a=2
	fi
	cat a | sed -n "/^$var/p"
done

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. Shell Programming and Scripting

For loop inside awk to read and print contents of files

Hello, I have a set of files Xfile0001 - Xfile0021, and the content of this files (one at a time) needs to be printed between some line (lines start with word "Generated") that I am extracting from another file called file7.txt and all the output goes into output.txt. First I tried creating a for... (5 Replies)
Discussion started by: jaldo0805
5 Replies

3. UNIX for Dummies Questions & Answers

Script to search for a particular word in files and print the word and path name

Hi, i am new to unix shell scripting and i need a script which would search for a particular word in all the files present in a directory. The output should have the word and file path name. For example: "word" "path name". Thanks for the reply in adv,:) (3 Replies)
Discussion started by: virtual_45
3 Replies

4. Shell Programming and Scripting

sed or awk to print 2nd last word

Hi, I have a file which has the following /usr/new/xyz/abc /us1/neb/yxr/def /usr/bin/cloud1/fgh /net/bin1/txt1/kdq I want to do something like this /usr/new/xyz/abc xyz /us1/neb/yxr/def yxr /usr/bin/cloud1/fgh cloud1 /net/bin1/txt1/kdq txt1 I need to add the 2nd last word to the... (3 Replies)
Discussion started by: matbrow
3 Replies

5. Shell Programming and Scripting

awk or sed command to print specific string between word and blank space

My source is on each line 98.194.245.255 - - "GET /disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=&drc=&mo=&sid=&lang=EN&loc=JPN HTTP/1.1" 302 - "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR... (5 Replies)
Discussion started by: elamurugu
5 Replies

6. Shell Programming and Scripting

How to Print from matching word to end using awk

Team, Could some one help me in Printing from matching word to end using awk For ex: Input: I am tester for now I am tester yesterday I am tester tomorrow O/p tester for now tester yesterday tester tomorrow i.e Starting from tester till end of sentence (5 Replies)
Discussion started by: mallak
5 Replies

7. Shell Programming and Scripting

search a word and print specific string using awk

Hi, I have list of directory paths in a variable and i want to delete those dirs and if dir does not exist then search that string and get the correct path from xml file after that delete the correct directory. i tried to use grep and it prints the entire line from the search.once i get the entire... (7 Replies)
Discussion started by: dragon.1431
7 Replies

8. Shell Programming and Scripting

Using awk to print line starting with particular word

Hi Geeks, Consider this line: driver=c:\folder1\folder2 The above line is contained in a variable say 'var' . I want to copy everything after 'driver=' in to another variable say var2. Please tell me how can this be done. (8 Replies)
Discussion started by: ajincoep
8 Replies

9. UNIX for Advanced & Expert Users

print contents of file2 for matching pattern in file1 - AWK

File1 row is same as column 2 in file 2. Also file 2 will either start with A, B or C. And 3rd column in file 2 is always F2. When column 2 of file 2 matches file1 column, print all those rows into a separate file. Here is an example. file 1: 100 103 104 108 file 2: ... (6 Replies)
Discussion started by: i.scientist
6 Replies

10. Shell Programming and Scripting

awk - print file contents except regex

Hello, I have a file which has user information. Each user has 2 variables with the same name like Email: testuser1 Email: testuser1@test.com Email: testuser2 Email: testuser2@test.com My intention is to delete the ones without the '@' symbol. When I run this statement awk '/^Email:/&&!/@/'... (6 Replies)
Discussion started by: rmsagar
6 Replies
Login or Register to Ask a Question