![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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
Last edited by bakunin; 11-14-2007 at 09:19 AM.. |
|
||||
|
Quote:
Code:
awk -F"\t" '{ print $NF }' filename
Code:
awk -F"\t" '{ print > $NF }' filename
|
![]() |
| Bookmarks |
| Tags |
| awk, sed |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|