LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 01-08-2008, 11:05 AM   #1
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,129

Rep: Reputation: 30
cut column


I have a file as below,

$vi myfile

aaa;20071217
bbb;20070404
ccc;20070254
"


I would like to have two script with requirement below

script 1 --> if I want to cut the column 9-12 of the first line , the output should be 1217 , can advise how to write a script to get the result ?

script 2 --> the result is 1217 , this is 17-Dec , if I would like the output is the format Dec 17 , how to write a script ?

ps. can I advise to write this two script , I hope this script only hv single line .

Last edited by ust; 01-08-2008 at 11:21 AM.
 
Old 01-08-2008, 11:23 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
A perl one-liner...
Code:
perl -e 'while( <> ){ print substr( $_,8,4 ),"\n"; }' myfile
--- rod.

Last edited by theNbomr; 01-08-2008 at 11:26 AM.
 
Old 01-08-2008, 12:38 PM   #3
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Assume the filename is NewFile

For part 1 (this is at least 2x as fast as using Perl, use the 'time' program to check for yourself):
Code:
cut -b 9-12 NewFile
output is:
Code:
1217
0404
0254
For part 2 (haven't tested speed of this vs Perl, because I don't know Perl, this time my money is on Perl, but I could write a small C program to do this and it will be faster):
Code:
while read line; do echo $(echo $line | cut -b 9-10 | rev)$(echo $line | cut -b 11-12 | rev) | rev; done < NewFile
output is:
Code:
1712
0404
5402

Last edited by H_TeXMeX_H; 01-08-2008 at 12:43 PM.
 
Old 01-08-2008, 01:53 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,452

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Not checked for timing, but this other one-liner does all
Code:
gawk -F\; '{print $2}' myfile | date -f- +"%b %d"
 
Old 01-08-2008, 07:14 PM   #5
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,129

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by colucix View Post
Not checked for timing, but this other one-liner does all
Code:
gawk -F\; '{print $2}' myfile | date -f- +"%b %d"
thx reply ,

this script should work fine ,

if the file format is as below , the 1200 is the time , can advise how to modify the script to get the same result ? Thx

aaa;200712171200
bbb;200704041200
ccc;200702541200
 
Old 01-08-2008, 07:16 PM   #6
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,129

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by colucix View Post
Not checked for timing, but this other one-liner does all
Code:
gawk -F\; '{print $2}' myfile | date -f- +"%b %d"
thx reply ,

I tried the script , but it do not find the FIRST line of the line , what I want to get is the date in FIRST line , the second ,if the file format is as below , the 1200 is the time , can advise how to modify the script to get the same result ? Thx in advance.

aaa;200712171200
bbb;200704041200
ccc;200702541200
 
Old 01-08-2008, 08:33 PM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,452

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Quote:
Originally Posted by ust View Post
I tried the script , but it do not find the FIRST line of the line , what I want to get is the date in FIRST line , the second ,if the file format is as below
Sorry, but I don't truly understand what you're saying: did you get an error when processing the first line of your file?

Regarding you're last example, you can simply extract a substring from the second field, like this
Code:
gawk -F\; '{print substr($2,1,8)}' myfile | date -f- +"%b %d"
Anyway you will get an error when processing the LAST line of your example, since the 54th of February has not been added to the calendar, yet!
 
Old 01-08-2008, 09:05 PM   #8
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,129

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by colucix View Post
Sorry, but I don't truly understand what you're saying: did you get an error when processing the first line of your file?

Regarding you're last example, you can simply extract a substring from the second field, like this
Code:
gawk -F\; '{print substr($2,1,8)}' myfile | date -f- +"%b %d"
Anyway you will get an error when processing the LAST line of your example, since the 54th of February has not been added to the calendar, yet!
thx reply ,

your script works , but the result is as below,
Dec 17
Apr 04
Mar 26


what I want the result is Dec 17

thx.
 
Old 01-08-2008, 09:14 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Quote:
Originally Posted by ust View Post
I have a file as below,

$vi myfile

aaa;20071217
bbb;20070404
ccc;20070254
"


I would like to have two script with requirement below

script 1 --> if I want to cut the column 9-12 of the first line , the output should be 1217 , can advise how to write a script to get the result ?
Code:
# cut -c9-12 file|head -1
Quote:
script 2 --> the result is 1217 , this is 17-Dec , if I would like the output is the format Dec 17 , how to write a script ?

ps. can I advise to write this two script , I hope this script only hv single line .
assuming you have GNU date
Code:
# date "+%b %d" -d `head -1 file|cut -d";" -f2`
Dec 17
 
Old 01-08-2008, 09:29 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Quote:
Originally Posted by H_TeXMeX_H View Post
Code:
while read line; do echo $(echo $line | cut -b 9-10 | rev)$(echo $line | cut -b 11-12 | rev) | rev; done < NewFile
output is:
Code:
1712
0404
5402

Code:
# sed 's/.*\(..\)\(..\)$/\2\1/' file
1712
0404
5402
 
Old 01-09-2008, 03:28 AM   #11
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Yeah, that is a better solution, and much faster too. Now, I'll have to look into how it works.
 
  


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
Column into rows bharatbsharma Programming 1 10-25-2007 02:23 AM
Extracting particular column value talat Programming 12 09-11-2007 03:33 AM
ls -> Column Order daletaylor Linux - Newbie 3 06-22-2007 10:59 AM
Erratic column? Hitboxx LQ Suggestions & Feedback 3 03-03-2007 10:53 AM
Column limit agallant Programming 1 08-05-2004 10:58 AM


All times are GMT -5. The time now is 11:15 PM.

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