The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to delete content in a file (delete content only) kittusri9 Shell Programming and Scripting 5 05-15-2008 01:12 PM
Split large file and add header and footer to each file ashish4422 Shell Programming and Scripting 1 04-15-2008 06:12 AM
Split file into multiple files depending upon first 4 digits deepakgang Shell Programming and Scripting 4 04-09-2008 01:21 AM
Split a file with no pattern -- Split, Csplit, Awk madhunk UNIX for Dummies Questions & Answers 10 12-17-2007 12:57 PM
Do commands depending on the comparision mr_bold UNIX for Dummies Questions & Answers 4 04-12-2007 06:31 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-14-2007
Chaitrali Chaitrali is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 9
split file depending on content

Hi,
I have a file which contains records of data.
I need to split the file into multiple files depending upon the value of last field.
How do i read the last field of each record in the file???

Regards,
Chaitrali
  #2 (permalink)  
Old 11-14-2007
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
what is the record separator in your file?

give some sample data in order to receive good responses
  #3 (permalink)  
Old 11-14-2007
Chaitrali Chaitrali is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 9
Hi Yogesh,

The record is tab separated......


Rgds,
Chairali.
  #4 (permalink)  
Old 11-14-2007
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
In this case the last field would be matched by a regexp (replace "<tab>" by a literal tab character):

sed 's/.*<tab>//' file

The reason why this works is because Unix regexps are "greedy": always the longest possible match is used. If you have several tabs in one input line ".*<tab>" will match all possible characters (including tabs!) followed by a tab char, which will be the rightmost one. Be sure to have no trailing tabs at the end of the line, though, as in this case the regexp would match the whole line.

To extract the rightmpost field from the line and split the file linewise into different files use something like:

Code:
cat file | while read line ; do
     lastfield="$(print $line | sed 's/.*<tab>//')"
     case $lastfield in
          abc)
               print - "$line" >> file1
               ;;

           def)
               print - "$line" >> file2
               ;;

           ghi)
               print - "$line" >> file3
               ;;

           *)
               print -u2 "i do not know where to put $line"
               ;;

     esac
done
bakunin

Last edited by bakunin; 11-14-2007 at 09:19 AM..
  #5 (permalink)  
Old 11-14-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Quote:
How do i read the last field of each record in the file???
Code:
awk -F"\t" '{ print $NF }' filename
to split files based on the last field value using the last field value as the split_filename

Code:
awk -F"\t" '{ print > $NF }' filename
Closed Thread

Bookmarks

Tags
awk, sed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:39 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0