Extracting specified line from a file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting specified line from a file using awk
# 1  
Old 07-15-2006
Extracting specified line from a file using awk

Hi,
I am just trying to extract a line at a time from a file using awk and can't yet extract even the first line using the following:-

awk '/(^[a-zA-Z0-9]) && (NR==1)/ {print $1}' file1

Is there an obvious silly error here?

Thanks
# 2  
Old 07-15-2006
Code:
awk ' {
        if (NR==1){
            if( $1 ~ /^[A-Za-z0-9]/) {print $0}
        }
}' file1

If I understand what you want. ^[stuff] means match this at the start of the line
or
Code:
sed '1q' file1 | grep '^[a-zA-Z0-9]'

# 3  
Old 07-15-2006
Code:
awk '/^[a-zA-Z0-9]/ && (NR==1) {print $1}' file1

# 4  
Old 07-15-2006
Thanks for the responses Jim and vgersh99.
Your moving of the / / of my line works thanks vgersh99.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

problem with extracting line in file

My file looks like this and i need to only extract those with PDT_AP21_B and output it to another file. Can anyone help? Thanks. PDT_AP21_R,,, 11 TYS,,,,T17D1207230742TYO***T17DS,,C PDT_AP21_L,,,9631166650001 ,,,,T17D1207230903TYOTYST17DS ,,C... (3 Replies)
Discussion started by: Alyssa
3 Replies

2. UNIX for Dummies Questions & Answers

Extracting line1 from a file with certain file pattern in line 7

Hello there, I am new to unix and would like to do the following, hoping someone would give some guide, thanks in advance. Lets say i have a file like this: A w x y w x 0.1 B w x y w x 0.3 C w x y w x 0.7 D w x y w x 0.9 E w x y w x 0.2 So i would like to extract line 1 data where line... (2 Replies)
Discussion started by: seiksoon
2 Replies

3. Shell Programming and Scripting

AWK - Extracting substr and using it on the same line

Hey, I'm new to the forums. I've searched around and found some great help on how to work with sub-strings but I'm having problems extracting a substr and then using it on the line it was extracted from :wall: I have a text file full of a varying amount of tables (each with the same number of... (7 Replies)
Discussion started by: pyro214
7 Replies

4. Shell Programming and Scripting

error while extracting a line from a file based on identifier

here is the content of input file CREATE TABLE `bla bla bla` ( `allianceSiteId` int(11) DEFAULT NULL, `trunkGroupsId` int(11) DEFAULT NULL, `lastModified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, UNIQUE KEY `allianceSiteId`... (4 Replies)
Discussion started by: vivek d r
4 Replies

5. Shell Programming and Scripting

extracting line numbers from a file and redirecting them to another file

i have applied the following command on a file named unix.txt that contains the string "linux from the text" grep -n -i -w "linux from the text" unix.txt and the result is 5:Today's Linux from the text systems are split into various branches, developed over time by AT&T as well as various... (5 Replies)
Discussion started by: arindamlive
5 Replies

6. Shell Programming and Scripting

Extracting a column from a file and merging with other file using awk

Hi All: I have following files: File 1: <header> text... text .. text .. text .. <\header> x y z ... File 2: <header> text... text .. text .. (4 Replies)
Discussion started by: mrn006
4 Replies

7. Shell Programming and Scripting

Extracting line from a file

Hi, I would like to extract a certain portion of sentences from a particular sentence i.e. to extract the last section embrace by input output Please advice. Cheers (6 Replies)
Discussion started by: dwgi32
6 Replies

8. UNIX for Dummies Questions & Answers

Help: Sorting and extracting a line from a file

I was proposed with the following problem: The file 'numbers' contains a list of numbers. Write a command to place the largest one of those numbers in the file 'largest' (there should be nothing else in that file). Do not use the 'head' command in your answer. I think if i used the sort... (1 Reply)
Discussion started by: stava
1 Replies

9. Shell Programming and Scripting

Extracting a line in a text file

If my file looks like this…. 10 20 30 and I want to take each line individually and put it in a variable so it can be read later in it's on individual test statement, how can I do that? I guess what I'm asking is how can I extract each line individually. Thanks (5 Replies)
Discussion started by: terryporter51
5 Replies

10. Shell Programming and Scripting

AWK - Extracting matched line

Hi all, I have one more query related to AWK. I have the following csv data: ,qwertyA, field1, field2, field3, field4, field5, field6 ,,,,,,,,,,,,,,,,,,,100,200 ,,,,,,,,,,,,,,,,,,,300,400 ,qwertyB, field1, field2, field3, field4, field5, field6 ,,,,,,,,,,,,,,,,,,,100,200... (9 Replies)
Discussion started by: not4google
9 Replies
Login or Register to Ask a Question