cut column | Unix Linux Forums | Shell Programming and Scripting

  Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 01-08-2008
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 28 March 2015, 10:08 AM EDT
Location: Foxborough, MA
Posts: 7,764
Thanks: 170
Thanked 633 Times in 591 Posts

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

Sponsored Links
    #3  
Old Unix and Linux 01-08-2008
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 28 March 2015, 9:32 AM EDT
Location: The Netherlands
Posts: 7,730
Thanks: 128
Thanked 544 Times in 512 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 Unix and Linux 01-08-2008
bobbygsk bobbygsk is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 4 February 2015, 2:55 PM EST
Posts: 147
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 Unix and Linux 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 Unix and Linux 01-09-2008
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 28 March 2015, 9:32 AM EDT
Location: The Netherlands
Posts: 7,730
Thanks: 128
Thanked 544 Times in 512 Posts

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

Regards
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 05:22 AM.