Linux - GeneralThis 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.
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.
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 ???
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
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
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'
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'
#!/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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.