LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   cut column (http://www.linuxquestions.org/questions/linux-newbie-8/cut-column-612021/)

ust 01-08-2008 12:05 PM

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 .

theNbomr 01-08-2008 12:23 PM

A perl one-liner...
Code:

perl -e 'while( <> ){ print substr( $_,8,4 ),"\n"; }' myfile
--- rod.

H_TeXMeX_H 01-08-2008 01:38 PM

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


colucix 01-08-2008 02:53 PM

Not checked for timing, but this other one-liner does all
Code:

gawk -F\; '{print $2}' myfile | date -f- +"%b %d"

ust 01-08-2008 08:14 PM

Quote:

Originally Posted by colucix (Post 3015840)
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

ust 01-08-2008 08:16 PM

Quote:

Originally Posted by colucix (Post 3015840)
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

colucix 01-08-2008 09:33 PM

Quote:

Originally Posted by ust (Post 3016099)
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! ;)

ust 01-08-2008 10:05 PM

Quote:

Originally Posted by colucix (Post 3016143)
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.

ghostdog74 01-08-2008 10:14 PM

Quote:

Originally Posted by ust (Post 3015664)
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


ghostdog74 01-08-2008 10:29 PM

Quote:

Originally Posted by H_TeXMeX_H (Post 3015753)
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


H_TeXMeX_H 01-09-2008 04:28 AM

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 10:26 AM.