Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-19-2006, 04:39 AM
|
#1
|
Member
Registered: Mar 2005
Location: Japan
Distribution: TurboLinux, RHEL, SUSE
Posts: 96
Rep:
|
How to split file , .. awk or split
Structure of my result file is following :
-----------
11
12
13
14
15
END
21
22
END
31
32
34
45
61
62
END
----------
How can I split it to three text file. File name is free, but indexed.
"END" is split string for files (end of each file).
I want to do it using AWK, but shell is OK.
Have you any idea ???
Last edited by ERBRMN; 07-19-2006 at 04:52 AM.
|
|
|
07-19-2006, 05:01 AM
|
#2
|
Member
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424
Rep:
|
The following will append the corresponding parts to file1, file2, ... . It will read the content from STDIN, so you may invoke it as "command < your_data_file" from the shell.
Code:
#!/bin/bash
FILENAME=file
INDEX=1
while read
do
if [[ "$REPLY" = "END" ]]
then
(( INDEX++ ))
else
echo "$REPLY" >> $FILENAME$INDEX
fi
done
|
|
|
07-19-2006, 05:58 AM
|
#3
|
Member
Registered: Mar 2005
Location: Japan
Distribution: TurboLinux, RHEL, SUSE
Posts: 96
Original Poster
Rep:
|
Quote:
Originally Posted by spirit receiver
The following will append the corresponding parts to file1, file2, ... . It will read the content from STDIN, so you may invoke it as "command < your_data_file" from the shell.
Code:
#!/bin/bash
FILENAME=file
INDEX=1
while read
do
if [[ "$REPLY" = "END" ]]
then
(( INDEX++ ))
else
echo "$REPLY" >> $FILENAME$INDEX
fi
done
|
Thank you very much, spirit receiver
It was good job.
|
|
|
07-20-2006, 10:32 PM
|
#4
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
|
Hello there. I still recommend spirit receiver's script the first but just in case you'll want to use this too.
command.sh file
Code:
#!/bin/bash
FILENAME="$1"
INDEX=1
IFS=$'\n'
for REPLY in $(<$FILENAME); do
if [[ "$REPLY" = "END" ]]; then
(( INDEX++ ))
else
echo "$REPLY" >> $FILENAME$INDEX
fi
done
#IFS=$' \t\n'
regards 
|
|
|
07-20-2006, 11:56 PM
|
#5
|
Member
Registered: Mar 2005
Location: Japan
Distribution: TurboLinux, RHEL, SUSE
Posts: 96
Original Poster
Rep:
|
Quote:
Originally Posted by konsolebox
Hello there. I still recommend spirit receiver's script the first but just in case you'll want to use this too.
command.sh file
Code:
#!/bin/bash
FILENAME="$1"
INDEX=1
IFS=$'\n'
for REPLY in $(<$FILENAME); do
if [[ "$REPLY" = "END" ]]; then
(( INDEX++ ))
else
echo "$REPLY" >> $FILENAME$INDEX
fi
done
#IFS=$' \t\n'
regards 
|
Thk you, konsolebox
This is also good job.
|
|
|
08-14-2006, 06:58 PM
|
#6
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
A tad late, and not quite perfect, but worth looking at :}
Code:
awk 'BEGIN{RS="END";OFS="\n"}{$1=$1; print > "myfile"NR}' test.txt
I haven't quite figured out why it will produce a 4th file (with only
and empty line in it) even though there's just three records.
Cheers,
Tink
|
|
|
08-14-2006, 07:21 PM
|
#7
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
Tinkster,
On my box, it only makes three files if there is no \n after the last END in the file.txt .
|
|
|
08-14-2006, 08:07 PM
|
#8
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Ok, looked at it again. Behaves correctly if I do RS="END\n"
Odd.
Cheers,
Tink
Last edited by Tinkster; 08-14-2006 at 08:09 PM.
|
|
|
08-14-2006, 09:37 PM
|
#9
|
Senior Member
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450
Rep:
|
Code:
#!/bin/sh
index=1
filebase=foo
while read LINE
do
if [ "${LINE}" = "END" ];
then
index=`expr ${index} + 1`
else
echo "${LINE}" >> ${filebase}.${index}
fi
done
Variation of the first response but is bourne shell compatible (/bin/sh) so it's portable on systems where bash is not in the base. I love bash but, with something this simple, bash specific functionality is not required to reduce complexity and only frustrates portability.  You can see it's not much different from the first reply; you just have to include a variable for the read and use expr (an external command) to do the math because there's no shell arithmatic in the original bourne shell.
Oh, and I named the files foo.# because that's my personal preference... easy to change if you want.
Can anyone tell I am bored?
Last edited by frob23; 08-14-2006 at 09:43 PM.
|
|
|
08-15-2006, 12:02 AM
|
#10
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Quote:
Originally Posted by Tinkster
Ok, looked at it again. Behaves correctly if I do RS="END\n"
Odd.
|
Actually, thinking about it some more (and I'm kind of slow due to
a flu :}) it's not that odd at all. If END is the delimiter, the \n does
comprise a 4th record that's unterminated.
Cheers,
Tink
|
|
|
All times are GMT -5. The time now is 08:33 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|