LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-20-2003, 09:51 AM   #1
ravykanth
Member
 
Registered: Sep 2003
Location: Houston
Distribution: Red Hat 9
Posts: 36

Rep: Reputation: 15
Question Script to read a simple text file


Hello,
Can anyone plz suggest a way to accomplish the following:
I have a semi-column ( separated text file which has coumn headers in the first line and the values in the lines that follow. I need a script that would skip the first line with the column headers and fetch me only the values that follow.

Thanks,
Ravi.
 
Old 10-20-2003, 10:41 AM   #2
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
awk 'NR > 1 {print $0}' <filename>

or

tail -n <filename> // where n is one less than the number of lines in the file
 
Old 10-20-2003, 11:06 AM   #3
ravykanth
Member
 
Registered: Sep 2003
Location: Houston
Distribution: Red Hat 9
Posts: 36

Original Poster
Rep: Reputation: 15
Hi,
The second command works...thanks. But just to understand: when I issue the first command it just goes to next line on the console...doesn' do anything. Am I doing something wrong? I issued the command:
awk 'NR > 1 {print $0} Test
where Test is the name of my file.

Thanks,
Ravi.
 
Old 10-20-2003, 11:08 AM   #4
ravykanth
Member
 
Registered: Sep 2003
Location: Houston
Distribution: Red Hat 9
Posts: 36

Original Poster
Rep: Reputation: 15
Sorry...my bad. I didn' give the second quote. Thanks for the help again.

Ravi.
 
Old 10-20-2003, 11:24 AM   #5
realos
Member
 
Registered: Jul 2002
Location: Germany
Distribution: Redhat 7.3, Debian 3.1, Knoppix 3.1, Ubuntu 6.10
Posts: 113

Rep: Reputation: 15
I wrote a small script in perl on the fly which solves the problem you have described.

filename: original file
newfilename: new file with first line of original file truncated


#! /usr/bin/env perl

open (INFILE, "filename")|| die ("File not found. Program exits!");
open (NEWFILE, ">newfilename") || die ("Could not create file _newfilename_. Program exits!");

my @file = <INFILE>;
print NEWFILE @file[1..$#file];
close(NEWFILE);
close(INFILE);


store the above given code in a file and execute it in the same directory as your original file.

cheers

Last edited by realos; 10-20-2003 at 11:32 AM.
 
Old 10-20-2003, 11:33 AM   #6
ravykanth
Member
 
Registered: Sep 2003
Location: Houston
Distribution: Red Hat 9
Posts: 36

Original Poster
Rep: Reputation: 15
Hi Luqman,
Can you plz reply to my earlier post on corrupt hard-disk problem. I know you suggested to use bootable cd and then re-mount root partition....but how do I mount the root partition after I boot using the bootable cd.

Thanks,
Ravi.
 
Old 10-21-2003, 08:13 AM   #7
realos
Member
 
Registered: Jul 2002
Location: Germany
Distribution: Redhat 7.3, Debian 3.1, Knoppix 3.1, Ubuntu 6.10
Posts: 113

Rep: Reputation: 15
@jkobrien:

your

"awk 'NR > 1 {print $0}' <filename>"

statement works fine with my system but shouldn't it be "gawk 'if (NR>1) print $0' <filename>" ?

I am a bit confused why your command works without if clause. Isn't general structure of awk commands like this?

/pattern/ STATEMENT

Can you please point to certain line of manpage, doc or explain in your own word.

thanks,

Last edited by realos; 10-21-2003 at 08:15 AM.
 
Old 10-21-2003, 08:26 AM   #8
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
Hi,

I don't really know how to answer your question. I've always used awk as

awk -switches 'pattern {command}' filename

"man awk" shows the same thing (gawk is just gnu awk). "if" is always implicit in the pattern and usually you don't need any switches.

I've never seen your syntax before and it gives me parse errors on my system. Does it work on your system?

rpm -q gawk -> gawk-3.1.1-9

John
 
Old 10-21-2003, 07:33 PM   #9
realos
Member
 
Registered: Jul 2002
Location: Germany
Distribution: Redhat 7.3, Debian 3.1, Knoppix 3.1, Ubuntu 6.10
Posts: 113

Rep: Reputation: 15
Yes my command was working at my work place with gawk 3.1.2 but at home an older version 3.1.1. gives syntax error.
 
Old 10-22-2003, 04:15 AM   #10
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
I just had a look at the gawk home pages and I realise now what you mean.

The two slashes signify a regular expression ("man awk" or the link above for more on these - they're for matching patterns). If you just specify a regexp, e.g. awk '/ABC/ {print $0}' file, awk will return any lines with the text "ABC" in them. As we're trying to return everything beyond the first record, regexp's aren't apropriate.

Also, the slashes and single quotes create difficulties with the t and c shells. In fact if you search the link above, you'll see that the author says "If you use csh, you're on your own".

John
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need something that can read a Microsoft Works text file joshknape Linux - Software 1 10-29-2005 11:45 PM
PHP Shell Script - Read piped text dlublink Linux - Software 3 08-13-2005 01:57 PM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
Read in an Octal number from a text file using C++ pjordan Programming 2 11-18-2004 03:03 PM
Display/Read line 'N' in a text file using script ganninu Linux - Newbie 2 10-13-2003 05:28 AM


All times are GMT -5. The time now is 05:00 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration