LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-19-2006, 02:28 AM   #1
George2
Member
 
Registered: Oct 2003
Posts: 354

Rep: Reputation: 30
file processing -- remove the first column of each line


Hello everyone,


I have a text file with a lot of lines, and in each line, there are several columns divided by space character, but the number of columns in each line varies. For example, here is a sample,

--------------------
abc 123 234 456
bsc 3 5 7 9 10 123
yyt 23
--------------------

I want to get a new file based on the existing file, by removing the first column, here is the related output of above sample,

--------------------
123 234 456
3 5 7 9 10 123
23
--------------------

Currently, my program runs on Linux, I am using awk command in my C/C++ program, to print from $2 to $100 by removing the first column of each line (I assume that the number of columns does not exceed 100).

I think my method is somewhat stupid. Does anyone have better ideas of how to utilize awk or sed or some other utility in my C/C++ program to do the same thing?


thanks in advance,
George
 
Old 11-19-2006, 02:34 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
awk '{for(i=2;i<NF;i++){printf "%s " $i};printf "\n"}' file


Cheers,
Tink
 
Old 11-19-2006, 03:13 AM   #3
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by Tinkster
awk '{for(i=2;i<NF;i++){printf "%s " $i};printf "\n"}' file


Cheers,
Tink
Thank you Tink!


regards,
George
 
Old 11-19-2006, 06:37 AM   #4
fvu
LQ Newbie
 
Registered: Nov 2006
Distribution: Ubuntu 7.10
Posts: 28

Rep: Reputation: 15
I had to use "<=" and "printf ," to get the awk solution to work in GNU Awk 3.1.4:

Code:
awk '{for(i=2;i<=NF;i++){printf "%s ", $i};printf "\n"}'
Shorter, faster (at least twice as fast, see time) and more readable(?) is a solution using cut or sed:

Code:
cut -d ' ' -f 2- file
sed 's/^[^ ]* //' < file
Greetings,

Freddy Vulto
http://www.fvue.nl/wiki

Last edited by fvu; 11-24-2006 at 01:34 AM.
 
Old 11-19-2006, 07:33 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Python alternative:
Code:
for lines in open("file.txt"):
    lines = lines.split()
    print ' '.join(lines[1:])
 
Old 11-19-2006, 08:24 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Quote:
Originally Posted by ghostdog74
Python alternative:
Code:
for lines in open("file.txt"):
    lines = lines.split()
    print ' '.join(lines[1:])
Damn, I wasn't going to get involved in this one, but if there's a python example, I feel obliged to do a perl one Just for fun, lets have two alternatives. First, the obfuscated-by-perl-syntax version:
Code:
perl -ne '/^(\S+)\s/ && print "$1\n"' file_to_filter
Then a more readable version:
Code:
perl -ne '@a=split(/\s/); print "$a[0]\n";' file_to_filter
 
Old 11-19-2006, 09:08 AM   #7
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 732

Rep: Reputation: 75
Hi, matthewg42.

Code:
perl -ne '/^(\S+)\s/ && print "$1\n"' file_to_filter
perl -ne '@a=split(/\s/); print "$a[0]\n";' file_to_filter
As they say on the game show: are these your final answers ? ... cheers, makyo

Quote:
Originally Posted by George2
I want to get a new file based on the existing file, by removing the first column, here is the related output of above sample,

--------------------
123 234 456
3 5 7 9 10 123
23
--------------------
 
Old 11-19-2006, 09:15 AM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Just re-direct the output to a new file
Code:
perl -ne '/^(\S+)\s/ && print "$1\n"' original_file > new_file
 
  


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
Show line and column numbers in Vim 7.0 judgex Linux - Desktop 1 08-29-2006 06:06 AM
c++ file processing -- how to remove a record from a file sharonyiisl Programming 4 09-26-2004 03:54 AM
remove 'modified date' column in nautilus list view nexx_au Linux - Software 0 12-09-2003 03:29 AM
PERL:: trying to remove last line of file ocularbob Programming 16 09-01-2003 01:07 PM
How to remove line of text from file netkepala Linux - General 2 05-23-2003 11:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:52 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration