cut column | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

cut column

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-08-2008
ust ust is offline
Registered User
 
Join Date: Feb 2005
Last Activity: 13 February 2014, 10:50 PM EST
Posts: 110
Thanks: 0
Thanked 0 Times in 0 Posts
cut column

I have a file as below,

$vi myfile

aaa;20071217
bbb;20070404
ccc;20070254
"



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 ? thx

p.s. can a script that have only ONE line could do that ?
Sponsored Links
    #2  
Old 01-08-2008
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 31 October 2014, 6:07 PM EDT
Location: Foxborough, MA
Posts: 7,667
Thanks: 152
Thanked 586 Times in 551 Posts

Code:
echo 'aaa;20071217' | sed 's/.*\(....\)/\1/'

Sponsored Links
    #3  
Old 01-08-2008
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 30 October 2014, 12:48 PM EDT
Location: The Netherlands
Posts: 7,719
Thanks: 123
Thanked 538 Times in 508 Posts
Quote:
Originally Posted by ust View Post
if I want to cut the column 9-12
Simply:


Code:
cut -c 9-12

or:


Code:
awk '{print substr($0, 9, 4)}'

Regards
    #4  
Old 01-08-2008
bobbygsk bobbygsk is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 17 July 2014, 9:49 AM EDT
Posts: 146
Thanks: 27
Thanked 0 Times in 0 Posts
<prompt>$ cut -b 9- myfile > cutfile
<prompt>$ cat cutfile
or
<prompt>$ cat myfile | cut -b 9-
Sponsored Links
    #5  
Old 01-08-2008
ust ust is offline
Registered User
 
Join Date: Feb 2005
Last Activity: 13 February 2014, 10:50 PM EST
Posts: 110
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Franklin52 View Post
Simply:


Code:
cut -c 9-12

or:


Code:
awk '{print substr($0, 9, 4)}'

Regards
thx reply ,

I tried it , the result is as below,

1217
0404
0254

if I only want the get the date in FIRST line , the result is 1217 , can advise how to modify the script ? thx
Sponsored Links
    #6  
Old 01-09-2008
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 30 October 2014, 12:48 PM EDT
Location: The Netherlands
Posts: 7,719
Thanks: 123
Thanked 538 Times in 508 Posts

Code:
awk 'NR==1{print substr($0, 9, 4);exit}'

Regards
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Rename a header column by adding another column entry to the header column name Vavad UNIX for Dummies Questions & Answers 1 08-06-2011 01:02 PM
Rename a header column by adding another column entry to the header column name URGENT!! Vavad Shell Programming and Scripting 4 08-05-2011 12:35 PM
Need to add letters to a column and add in a new column subtracting from another column kellywilliams Shell Programming and Scripting 3 01-27-2011 09:50 PM
Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 rydz00 Shell Programming and Scripting 7 11-09-2010 10:28 AM
Changing one column of delimited file column to fixed width column manneni prakash Shell Programming and Scripting 5 06-22-2009 05:27 AM



All times are GMT -4. The time now is 12:39 AM.