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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-08-2008, 11:05 AM
|
#1
|
Senior Member
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130
Rep:
|
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.
|
|
|
01-08-2008, 11:23 AM
|
#2
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
|
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.
|
|
|
01-08-2008, 12:38 PM
|
#3
|
LQ Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
|
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:
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:
Last edited by H_TeXMeX_H; 01-08-2008 at 12:43 PM.
|
|
|
01-08-2008, 01:53 PM
|
#4
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Not checked for timing, but this other one-liner does all
Code:
gawk -F\; '{print $2}' myfile | date -f- +"%b %d"
|
|
|
01-08-2008, 07:14 PM
|
#5
|
Senior Member
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
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
|
|
|
01-08-2008, 07:16 PM
|
#6
|
Senior Member
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
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
|
|
|
01-08-2008, 08:33 PM
|
#7
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by ust
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! 
|
|
|
01-08-2008, 09:05 PM
|
#8
|
Senior Member
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
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.
|
|
|
01-08-2008, 09:14 PM
|
#9
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by ust
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
|
|
|
01-08-2008, 09:29 PM
|
#10
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by H_TeXMeX_H
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:
# sed 's/.*\(..\)\(..\)$/\2\1/' file
1712
0404
5402
|
|
|
01-09-2008, 03:28 AM
|
#11
|
LQ Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
|
Yeah, that is a better solution, and much faster too. Now, I'll have to look into how it works.
|
|
|
All times are GMT -5. The time now is 06:35 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|