Split the content of a file in linux
I have a text file which has content like this:
abc.tar^@xxx.tar^@yyy.tar^@
Say for example I have this content in a file named abc.txt and I want to split the content and write the first two entries into a new file.
(e.g), the new file would look like this:
abc.tar^@xxx.tar^@
Is there any command to perform this operation?
files text-processing
add a comment |
I have a text file which has content like this:
abc.tar^@xxx.tar^@yyy.tar^@
Say for example I have this content in a file named abc.txt and I want to split the content and write the first two entries into a new file.
(e.g), the new file would look like this:
abc.tar^@xxx.tar^@
Is there any command to perform this operation?
files text-processing
2
are those the two characters^and@? Or the NUL character (^@aka).
– Stéphane Chazelas
Aug 5 '13 at 17:12
In such case my awk goes (I think): awk -F"x00" '{print $1"x00"$2"x00"}' abc.txt > newfile.txt
– GermanG
Aug 5 '13 at 18:12
add a comment |
I have a text file which has content like this:
abc.tar^@xxx.tar^@yyy.tar^@
Say for example I have this content in a file named abc.txt and I want to split the content and write the first two entries into a new file.
(e.g), the new file would look like this:
abc.tar^@xxx.tar^@
Is there any command to perform this operation?
files text-processing
I have a text file which has content like this:
abc.tar^@xxx.tar^@yyy.tar^@
Say for example I have this content in a file named abc.txt and I want to split the content and write the first two entries into a new file.
(e.g), the new file would look like this:
abc.tar^@xxx.tar^@
Is there any command to perform this operation?
files text-processing
files text-processing
edited 4 hours ago
Rui F Ribeiro
39.2k1479130
39.2k1479130
asked Aug 5 '13 at 15:38
Mano
1392510
1392510
2
are those the two characters^and@? Or the NUL character (^@aka).
– Stéphane Chazelas
Aug 5 '13 at 17:12
In such case my awk goes (I think): awk -F"x00" '{print $1"x00"$2"x00"}' abc.txt > newfile.txt
– GermanG
Aug 5 '13 at 18:12
add a comment |
2
are those the two characters^and@? Or the NUL character (^@aka).
– Stéphane Chazelas
Aug 5 '13 at 17:12
In such case my awk goes (I think): awk -F"x00" '{print $1"x00"$2"x00"}' abc.txt > newfile.txt
– GermanG
Aug 5 '13 at 18:12
2
2
are those the two characters
^ and @? Or the NUL character (^@ aka ).– Stéphane Chazelas
Aug 5 '13 at 17:12
are those the two characters
^ and @? Or the NUL character (^@ aka ).– Stéphane Chazelas
Aug 5 '13 at 17:12
In such case my awk goes (I think): awk -F"x00" '{print $1"x00"$2"x00"}' abc.txt > newfile.txt
– GermanG
Aug 5 '13 at 18:12
In such case my awk goes (I think): awk -F"x00" '{print $1"x00"$2"x00"}' abc.txt > newfile.txt
– GermanG
Aug 5 '13 at 18:12
add a comment |
5 Answers
5
active
oldest
votes
I'm guessing this question is related to that one, correct?
In that case, wouldn't replacing the '^@' with a newline be more worth your while?
In the following, I'm guessing you mean '^@', the ASCII NUL byte:
$ sed 's/o000/n/g' abc.txt | head -n 2
abc.tar
xxx.tar
So you need
sed 's/o000/n/g' abc.txt | head -n 2 > newfile.txt
Explanation
This substitutes a newline (n) for every NUL byte (o000) the o part means that what follows is a byte in octal notation. The output is then piped to head -n 2 which extracts the first two lines; and the resulting lines are redirected (>) to the file newfile.txt.
If it's important to you that the file names be separated by '^@', however, you can use this:
perl -nl000 -e '
$num_lines =2 ;
push @a,(split /00/)[0..$num_lines-1];
print $_ for @a' abc.txt > newfile.txt
Replace the value of $num_lines above as needed to grab the first $num_lines lines from the file.
Explanation
- The
-nswitch tellsperlto run the code on each line of the input file - The
-l000sequence tellsperlto set the output record separator
(the character printed after every string) to the NUL byte (000). - The
-eswitch tellsperlthat the string that follows is a code to execute. - The
splitfunction splits each input line with the NUL byte as delimiter,takes the first$num_lines([0..$num_lines-1]) results and puts them into the array@a. Notice that the "current input line" part is nowhere specified in the function call. This makes use of the fact that the default scalar variable in Perl ($_) is the default argument of thesplitfunction (among others) when no argument is supplied. - The final
foreachloop prints every element in@a(again note how$_is the default iterator for theforeachloop). Since we have set the output record separator to octal000, we get the results separated by the NUL byte as before.
Thanks @Joseph R , But can you pls tell me how can i use the command in through java?? since i need to pass it in double quotes like "sed 's/o000/n/g' abc.txt | head -n 2 > newfile.txt" its showing error for the sequences.
– Mano
Aug 7 '13 at 5:46
@user1752557 What errors do you get?
– Joseph R.
Aug 7 '13 at 9:58
add a comment |
Is this:
awk -F"@" '{print $1"@"$2"@"}' abc.txt > newfile.txt
good enough for you?
The question is about records separated by null bytes, not by@characters.
– Gilles
Aug 6 '13 at 1:22
Copying my own comment: In such case my awk goes (I think): awk -F"x00" '{print $1"x00"$2"x00"}' abc.txt > newfile.txt
– GermanG
Aug 6 '13 at 14:38
add a comment |
Try running:
sed -r -i 's/^(.*)@.*@.*$/1/' file
What are you trying to match with(.*)(.*)? Also why usecat? You can dosed -r 'pattern_here' filedirectly. Finally, this code seems to assume that the input file only has 3 file names of which you extract the first one only.
– Joseph R.
Aug 5 '13 at 17:37
.*should match on anything, the idea is to split the contents of the file using@as a delimiter. And why not usecat--it's just as viable a method as any other.
– Alexej Magura
Aug 5 '13 at 18:14
My point is,(.*)(.*)will not yield any different result from(.*)why did you repeat it? As for why not usecat, it's not about functionality, it's about tacking on unneeded complexity. This is not whatcatwas made for anyway.
– Joseph R.
Aug 5 '13 at 18:17
I think that I may have forgotten to try the above without the extra(.*)and so that may be why there's an extra one. If you wish, feel free to edit the above.
– Alexej Magura
Aug 5 '13 at 18:19
Done. I'm leaving thecatup to your discretion :)
– Joseph R.
Aug 5 '13 at 18:22
add a comment |
Here's an example using Perl:
$ perl -ne '@F = split(/@/,$_); print "$F[1]@$F[2]@";' abc.txt > newfile.txt
The above does the following:
@F = split(/@/,$_)- splits the contents of fileabc.txta line at a time up based on the character@and stores the resulting chunks in an array (@F).
print "$F[1]@$F[2]@"- prints the first 2 columns, (1 & 2), from array@Fand inserts a at sign (@) in between each column.
add a comment |
Awk can use any character as the record separator (with newline as the default), except that some implementations don't support the null byte as the separator. Gawk (GNU awk), the default awk on most non-embedded Linux installations, supports nulls.
gawk -v RS='' -v ORS='' 'NR <= 2 {print}'
This can be shortened to gawk -v RS='' -v ORS='' 'NR <= 2' since printing the record is the default action.
For a large file, you'd better quit after the second line.
gawk -v RS='' -v ORS='' 'NR==3 {exit} {print}'
Alternatively, you can use head. There's no option to use a null byte instead of a newline as the record separator, but you can swap the two characters, call head, and then swap back.
tr 'n' 'n' | head -n 2 | tr 'n' 'n'
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f85585%2fsplit-the-content-of-a-file-in-linux%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm guessing this question is related to that one, correct?
In that case, wouldn't replacing the '^@' with a newline be more worth your while?
In the following, I'm guessing you mean '^@', the ASCII NUL byte:
$ sed 's/o000/n/g' abc.txt | head -n 2
abc.tar
xxx.tar
So you need
sed 's/o000/n/g' abc.txt | head -n 2 > newfile.txt
Explanation
This substitutes a newline (n) for every NUL byte (o000) the o part means that what follows is a byte in octal notation. The output is then piped to head -n 2 which extracts the first two lines; and the resulting lines are redirected (>) to the file newfile.txt.
If it's important to you that the file names be separated by '^@', however, you can use this:
perl -nl000 -e '
$num_lines =2 ;
push @a,(split /00/)[0..$num_lines-1];
print $_ for @a' abc.txt > newfile.txt
Replace the value of $num_lines above as needed to grab the first $num_lines lines from the file.
Explanation
- The
-nswitch tellsperlto run the code on each line of the input file - The
-l000sequence tellsperlto set the output record separator
(the character printed after every string) to the NUL byte (000). - The
-eswitch tellsperlthat the string that follows is a code to execute. - The
splitfunction splits each input line with the NUL byte as delimiter,takes the first$num_lines([0..$num_lines-1]) results and puts them into the array@a. Notice that the "current input line" part is nowhere specified in the function call. This makes use of the fact that the default scalar variable in Perl ($_) is the default argument of thesplitfunction (among others) when no argument is supplied. - The final
foreachloop prints every element in@a(again note how$_is the default iterator for theforeachloop). Since we have set the output record separator to octal000, we get the results separated by the NUL byte as before.
Thanks @Joseph R , But can you pls tell me how can i use the command in through java?? since i need to pass it in double quotes like "sed 's/o000/n/g' abc.txt | head -n 2 > newfile.txt" its showing error for the sequences.
– Mano
Aug 7 '13 at 5:46
@user1752557 What errors do you get?
– Joseph R.
Aug 7 '13 at 9:58
add a comment |
I'm guessing this question is related to that one, correct?
In that case, wouldn't replacing the '^@' with a newline be more worth your while?
In the following, I'm guessing you mean '^@', the ASCII NUL byte:
$ sed 's/o000/n/g' abc.txt | head -n 2
abc.tar
xxx.tar
So you need
sed 's/o000/n/g' abc.txt | head -n 2 > newfile.txt
Explanation
This substitutes a newline (n) for every NUL byte (o000) the o part means that what follows is a byte in octal notation. The output is then piped to head -n 2 which extracts the first two lines; and the resulting lines are redirected (>) to the file newfile.txt.
If it's important to you that the file names be separated by '^@', however, you can use this:
perl -nl000 -e '
$num_lines =2 ;
push @a,(split /00/)[0..$num_lines-1];
print $_ for @a' abc.txt > newfile.txt
Replace the value of $num_lines above as needed to grab the first $num_lines lines from the file.
Explanation
- The
-nswitch tellsperlto run the code on each line of the input file - The
-l000sequence tellsperlto set the output record separator
(the character printed after every string) to the NUL byte (000). - The
-eswitch tellsperlthat the string that follows is a code to execute. - The
splitfunction splits each input line with the NUL byte as delimiter,takes the first$num_lines([0..$num_lines-1]) results and puts them into the array@a. Notice that the "current input line" part is nowhere specified in the function call. This makes use of the fact that the default scalar variable in Perl ($_) is the default argument of thesplitfunction (among others) when no argument is supplied. - The final
foreachloop prints every element in@a(again note how$_is the default iterator for theforeachloop). Since we have set the output record separator to octal000, we get the results separated by the NUL byte as before.
Thanks @Joseph R , But can you pls tell me how can i use the command in through java?? since i need to pass it in double quotes like "sed 's/o000/n/g' abc.txt | head -n 2 > newfile.txt" its showing error for the sequences.
– Mano
Aug 7 '13 at 5:46
@user1752557 What errors do you get?
– Joseph R.
Aug 7 '13 at 9:58
add a comment |
I'm guessing this question is related to that one, correct?
In that case, wouldn't replacing the '^@' with a newline be more worth your while?
In the following, I'm guessing you mean '^@', the ASCII NUL byte:
$ sed 's/o000/n/g' abc.txt | head -n 2
abc.tar
xxx.tar
So you need
sed 's/o000/n/g' abc.txt | head -n 2 > newfile.txt
Explanation
This substitutes a newline (n) for every NUL byte (o000) the o part means that what follows is a byte in octal notation. The output is then piped to head -n 2 which extracts the first two lines; and the resulting lines are redirected (>) to the file newfile.txt.
If it's important to you that the file names be separated by '^@', however, you can use this:
perl -nl000 -e '
$num_lines =2 ;
push @a,(split /00/)[0..$num_lines-1];
print $_ for @a' abc.txt > newfile.txt
Replace the value of $num_lines above as needed to grab the first $num_lines lines from the file.
Explanation
- The
-nswitch tellsperlto run the code on each line of the input file - The
-l000sequence tellsperlto set the output record separator
(the character printed after every string) to the NUL byte (000). - The
-eswitch tellsperlthat the string that follows is a code to execute. - The
splitfunction splits each input line with the NUL byte as delimiter,takes the first$num_lines([0..$num_lines-1]) results and puts them into the array@a. Notice that the "current input line" part is nowhere specified in the function call. This makes use of the fact that the default scalar variable in Perl ($_) is the default argument of thesplitfunction (among others) when no argument is supplied. - The final
foreachloop prints every element in@a(again note how$_is the default iterator for theforeachloop). Since we have set the output record separator to octal000, we get the results separated by the NUL byte as before.
I'm guessing this question is related to that one, correct?
In that case, wouldn't replacing the '^@' with a newline be more worth your while?
In the following, I'm guessing you mean '^@', the ASCII NUL byte:
$ sed 's/o000/n/g' abc.txt | head -n 2
abc.tar
xxx.tar
So you need
sed 's/o000/n/g' abc.txt | head -n 2 > newfile.txt
Explanation
This substitutes a newline (n) for every NUL byte (o000) the o part means that what follows is a byte in octal notation. The output is then piped to head -n 2 which extracts the first two lines; and the resulting lines are redirected (>) to the file newfile.txt.
If it's important to you that the file names be separated by '^@', however, you can use this:
perl -nl000 -e '
$num_lines =2 ;
push @a,(split /00/)[0..$num_lines-1];
print $_ for @a' abc.txt > newfile.txt
Replace the value of $num_lines above as needed to grab the first $num_lines lines from the file.
Explanation
- The
-nswitch tellsperlto run the code on each line of the input file - The
-l000sequence tellsperlto set the output record separator
(the character printed after every string) to the NUL byte (000). - The
-eswitch tellsperlthat the string that follows is a code to execute. - The
splitfunction splits each input line with the NUL byte as delimiter,takes the first$num_lines([0..$num_lines-1]) results and puts them into the array@a. Notice that the "current input line" part is nowhere specified in the function call. This makes use of the fact that the default scalar variable in Perl ($_) is the default argument of thesplitfunction (among others) when no argument is supplied. - The final
foreachloop prints every element in@a(again note how$_is the default iterator for theforeachloop). Since we have set the output record separator to octal000, we get the results separated by the NUL byte as before.
edited Apr 13 '17 at 12:36
Community♦
1
1
answered Aug 5 '13 at 16:54
Joseph R.
28k373114
28k373114
Thanks @Joseph R , But can you pls tell me how can i use the command in through java?? since i need to pass it in double quotes like "sed 's/o000/n/g' abc.txt | head -n 2 > newfile.txt" its showing error for the sequences.
– Mano
Aug 7 '13 at 5:46
@user1752557 What errors do you get?
– Joseph R.
Aug 7 '13 at 9:58
add a comment |
Thanks @Joseph R , But can you pls tell me how can i use the command in through java?? since i need to pass it in double quotes like "sed 's/o000/n/g' abc.txt | head -n 2 > newfile.txt" its showing error for the sequences.
– Mano
Aug 7 '13 at 5:46
@user1752557 What errors do you get?
– Joseph R.
Aug 7 '13 at 9:58
Thanks @Joseph R , But can you pls tell me how can i use the command in through java?? since i need to pass it in double quotes like "sed 's/o000/n/g' abc.txt | head -n 2 > newfile.txt" its showing error for the sequences.
– Mano
Aug 7 '13 at 5:46
Thanks @Joseph R , But can you pls tell me how can i use the command in through java?? since i need to pass it in double quotes like "sed 's/o000/n/g' abc.txt | head -n 2 > newfile.txt" its showing error for the sequences.
– Mano
Aug 7 '13 at 5:46
@user1752557 What errors do you get?
– Joseph R.
Aug 7 '13 at 9:58
@user1752557 What errors do you get?
– Joseph R.
Aug 7 '13 at 9:58
add a comment |
Is this:
awk -F"@" '{print $1"@"$2"@"}' abc.txt > newfile.txt
good enough for you?
The question is about records separated by null bytes, not by@characters.
– Gilles
Aug 6 '13 at 1:22
Copying my own comment: In such case my awk goes (I think): awk -F"x00" '{print $1"x00"$2"x00"}' abc.txt > newfile.txt
– GermanG
Aug 6 '13 at 14:38
add a comment |
Is this:
awk -F"@" '{print $1"@"$2"@"}' abc.txt > newfile.txt
good enough for you?
The question is about records separated by null bytes, not by@characters.
– Gilles
Aug 6 '13 at 1:22
Copying my own comment: In such case my awk goes (I think): awk -F"x00" '{print $1"x00"$2"x00"}' abc.txt > newfile.txt
– GermanG
Aug 6 '13 at 14:38
add a comment |
Is this:
awk -F"@" '{print $1"@"$2"@"}' abc.txt > newfile.txt
good enough for you?
Is this:
awk -F"@" '{print $1"@"$2"@"}' abc.txt > newfile.txt
good enough for you?
edited Aug 5 '13 at 16:09
Anthon
60.3k17102163
60.3k17102163
answered Aug 5 '13 at 15:45
GermanG
841
841
The question is about records separated by null bytes, not by@characters.
– Gilles
Aug 6 '13 at 1:22
Copying my own comment: In such case my awk goes (I think): awk -F"x00" '{print $1"x00"$2"x00"}' abc.txt > newfile.txt
– GermanG
Aug 6 '13 at 14:38
add a comment |
The question is about records separated by null bytes, not by@characters.
– Gilles
Aug 6 '13 at 1:22
Copying my own comment: In such case my awk goes (I think): awk -F"x00" '{print $1"x00"$2"x00"}' abc.txt > newfile.txt
– GermanG
Aug 6 '13 at 14:38
The question is about records separated by null bytes, not by
@ characters.– Gilles
Aug 6 '13 at 1:22
The question is about records separated by null bytes, not by
@ characters.– Gilles
Aug 6 '13 at 1:22
Copying my own comment: In such case my awk goes (I think): awk -F"x00" '{print $1"x00"$2"x00"}' abc.txt > newfile.txt
– GermanG
Aug 6 '13 at 14:38
Copying my own comment: In such case my awk goes (I think): awk -F"x00" '{print $1"x00"$2"x00"}' abc.txt > newfile.txt
– GermanG
Aug 6 '13 at 14:38
add a comment |
Try running:
sed -r -i 's/^(.*)@.*@.*$/1/' file
What are you trying to match with(.*)(.*)? Also why usecat? You can dosed -r 'pattern_here' filedirectly. Finally, this code seems to assume that the input file only has 3 file names of which you extract the first one only.
– Joseph R.
Aug 5 '13 at 17:37
.*should match on anything, the idea is to split the contents of the file using@as a delimiter. And why not usecat--it's just as viable a method as any other.
– Alexej Magura
Aug 5 '13 at 18:14
My point is,(.*)(.*)will not yield any different result from(.*)why did you repeat it? As for why not usecat, it's not about functionality, it's about tacking on unneeded complexity. This is not whatcatwas made for anyway.
– Joseph R.
Aug 5 '13 at 18:17
I think that I may have forgotten to try the above without the extra(.*)and so that may be why there's an extra one. If you wish, feel free to edit the above.
– Alexej Magura
Aug 5 '13 at 18:19
Done. I'm leaving thecatup to your discretion :)
– Joseph R.
Aug 5 '13 at 18:22
add a comment |
Try running:
sed -r -i 's/^(.*)@.*@.*$/1/' file
What are you trying to match with(.*)(.*)? Also why usecat? You can dosed -r 'pattern_here' filedirectly. Finally, this code seems to assume that the input file only has 3 file names of which you extract the first one only.
– Joseph R.
Aug 5 '13 at 17:37
.*should match on anything, the idea is to split the contents of the file using@as a delimiter. And why not usecat--it's just as viable a method as any other.
– Alexej Magura
Aug 5 '13 at 18:14
My point is,(.*)(.*)will not yield any different result from(.*)why did you repeat it? As for why not usecat, it's not about functionality, it's about tacking on unneeded complexity. This is not whatcatwas made for anyway.
– Joseph R.
Aug 5 '13 at 18:17
I think that I may have forgotten to try the above without the extra(.*)and so that may be why there's an extra one. If you wish, feel free to edit the above.
– Alexej Magura
Aug 5 '13 at 18:19
Done. I'm leaving thecatup to your discretion :)
– Joseph R.
Aug 5 '13 at 18:22
add a comment |
Try running:
sed -r -i 's/^(.*)@.*@.*$/1/' file
Try running:
sed -r -i 's/^(.*)@.*@.*$/1/' file
edited Aug 5 '13 at 18:25
answered Aug 5 '13 at 15:49
Alexej Magura
1,56561533
1,56561533
What are you trying to match with(.*)(.*)? Also why usecat? You can dosed -r 'pattern_here' filedirectly. Finally, this code seems to assume that the input file only has 3 file names of which you extract the first one only.
– Joseph R.
Aug 5 '13 at 17:37
.*should match on anything, the idea is to split the contents of the file using@as a delimiter. And why not usecat--it's just as viable a method as any other.
– Alexej Magura
Aug 5 '13 at 18:14
My point is,(.*)(.*)will not yield any different result from(.*)why did you repeat it? As for why not usecat, it's not about functionality, it's about tacking on unneeded complexity. This is not whatcatwas made for anyway.
– Joseph R.
Aug 5 '13 at 18:17
I think that I may have forgotten to try the above without the extra(.*)and so that may be why there's an extra one. If you wish, feel free to edit the above.
– Alexej Magura
Aug 5 '13 at 18:19
Done. I'm leaving thecatup to your discretion :)
– Joseph R.
Aug 5 '13 at 18:22
add a comment |
What are you trying to match with(.*)(.*)? Also why usecat? You can dosed -r 'pattern_here' filedirectly. Finally, this code seems to assume that the input file only has 3 file names of which you extract the first one only.
– Joseph R.
Aug 5 '13 at 17:37
.*should match on anything, the idea is to split the contents of the file using@as a delimiter. And why not usecat--it's just as viable a method as any other.
– Alexej Magura
Aug 5 '13 at 18:14
My point is,(.*)(.*)will not yield any different result from(.*)why did you repeat it? As for why not usecat, it's not about functionality, it's about tacking on unneeded complexity. This is not whatcatwas made for anyway.
– Joseph R.
Aug 5 '13 at 18:17
I think that I may have forgotten to try the above without the extra(.*)and so that may be why there's an extra one. If you wish, feel free to edit the above.
– Alexej Magura
Aug 5 '13 at 18:19
Done. I'm leaving thecatup to your discretion :)
– Joseph R.
Aug 5 '13 at 18:22
What are you trying to match with
(.*)(.*)? Also why use cat? You can do sed -r 'pattern_here' file directly. Finally, this code seems to assume that the input file only has 3 file names of which you extract the first one only.– Joseph R.
Aug 5 '13 at 17:37
What are you trying to match with
(.*)(.*)? Also why use cat? You can do sed -r 'pattern_here' file directly. Finally, this code seems to assume that the input file only has 3 file names of which you extract the first one only.– Joseph R.
Aug 5 '13 at 17:37
.* should match on anything, the idea is to split the contents of the file using @ as a delimiter. And why not use cat--it's just as viable a method as any other.– Alexej Magura
Aug 5 '13 at 18:14
.* should match on anything, the idea is to split the contents of the file using @ as a delimiter. And why not use cat--it's just as viable a method as any other.– Alexej Magura
Aug 5 '13 at 18:14
My point is,
(.*)(.*) will not yield any different result from (.*) why did you repeat it? As for why not use cat, it's not about functionality, it's about tacking on unneeded complexity. This is not what cat was made for anyway.– Joseph R.
Aug 5 '13 at 18:17
My point is,
(.*)(.*) will not yield any different result from (.*) why did you repeat it? As for why not use cat, it's not about functionality, it's about tacking on unneeded complexity. This is not what cat was made for anyway.– Joseph R.
Aug 5 '13 at 18:17
I think that I may have forgotten to try the above without the extra
(.*) and so that may be why there's an extra one. If you wish, feel free to edit the above.– Alexej Magura
Aug 5 '13 at 18:19
I think that I may have forgotten to try the above without the extra
(.*) and so that may be why there's an extra one. If you wish, feel free to edit the above.– Alexej Magura
Aug 5 '13 at 18:19
Done. I'm leaving the
cat up to your discretion :)– Joseph R.
Aug 5 '13 at 18:22
Done. I'm leaving the
cat up to your discretion :)– Joseph R.
Aug 5 '13 at 18:22
add a comment |
Here's an example using Perl:
$ perl -ne '@F = split(/@/,$_); print "$F[1]@$F[2]@";' abc.txt > newfile.txt
The above does the following:
@F = split(/@/,$_)- splits the contents of fileabc.txta line at a time up based on the character@and stores the resulting chunks in an array (@F).
print "$F[1]@$F[2]@"- prints the first 2 columns, (1 & 2), from array@Fand inserts a at sign (@) in between each column.
add a comment |
Here's an example using Perl:
$ perl -ne '@F = split(/@/,$_); print "$F[1]@$F[2]@";' abc.txt > newfile.txt
The above does the following:
@F = split(/@/,$_)- splits the contents of fileabc.txta line at a time up based on the character@and stores the resulting chunks in an array (@F).
print "$F[1]@$F[2]@"- prints the first 2 columns, (1 & 2), from array@Fand inserts a at sign (@) in between each column.
add a comment |
Here's an example using Perl:
$ perl -ne '@F = split(/@/,$_); print "$F[1]@$F[2]@";' abc.txt > newfile.txt
The above does the following:
@F = split(/@/,$_)- splits the contents of fileabc.txta line at a time up based on the character@and stores the resulting chunks in an array (@F).
print "$F[1]@$F[2]@"- prints the first 2 columns, (1 & 2), from array@Fand inserts a at sign (@) in between each column.
Here's an example using Perl:
$ perl -ne '@F = split(/@/,$_); print "$F[1]@$F[2]@";' abc.txt > newfile.txt
The above does the following:
@F = split(/@/,$_)- splits the contents of fileabc.txta line at a time up based on the character@and stores the resulting chunks in an array (@F).
print "$F[1]@$F[2]@"- prints the first 2 columns, (1 & 2), from array@Fand inserts a at sign (@) in between each column.
answered Aug 5 '13 at 17:10
slm♦
247k66513678
247k66513678
add a comment |
add a comment |
Awk can use any character as the record separator (with newline as the default), except that some implementations don't support the null byte as the separator. Gawk (GNU awk), the default awk on most non-embedded Linux installations, supports nulls.
gawk -v RS='' -v ORS='' 'NR <= 2 {print}'
This can be shortened to gawk -v RS='' -v ORS='' 'NR <= 2' since printing the record is the default action.
For a large file, you'd better quit after the second line.
gawk -v RS='' -v ORS='' 'NR==3 {exit} {print}'
Alternatively, you can use head. There's no option to use a null byte instead of a newline as the record separator, but you can swap the two characters, call head, and then swap back.
tr 'n' 'n' | head -n 2 | tr 'n' 'n'
add a comment |
Awk can use any character as the record separator (with newline as the default), except that some implementations don't support the null byte as the separator. Gawk (GNU awk), the default awk on most non-embedded Linux installations, supports nulls.
gawk -v RS='' -v ORS='' 'NR <= 2 {print}'
This can be shortened to gawk -v RS='' -v ORS='' 'NR <= 2' since printing the record is the default action.
For a large file, you'd better quit after the second line.
gawk -v RS='' -v ORS='' 'NR==3 {exit} {print}'
Alternatively, you can use head. There's no option to use a null byte instead of a newline as the record separator, but you can swap the two characters, call head, and then swap back.
tr 'n' 'n' | head -n 2 | tr 'n' 'n'
add a comment |
Awk can use any character as the record separator (with newline as the default), except that some implementations don't support the null byte as the separator. Gawk (GNU awk), the default awk on most non-embedded Linux installations, supports nulls.
gawk -v RS='' -v ORS='' 'NR <= 2 {print}'
This can be shortened to gawk -v RS='' -v ORS='' 'NR <= 2' since printing the record is the default action.
For a large file, you'd better quit after the second line.
gawk -v RS='' -v ORS='' 'NR==3 {exit} {print}'
Alternatively, you can use head. There's no option to use a null byte instead of a newline as the record separator, but you can swap the two characters, call head, and then swap back.
tr 'n' 'n' | head -n 2 | tr 'n' 'n'
Awk can use any character as the record separator (with newline as the default), except that some implementations don't support the null byte as the separator. Gawk (GNU awk), the default awk on most non-embedded Linux installations, supports nulls.
gawk -v RS='' -v ORS='' 'NR <= 2 {print}'
This can be shortened to gawk -v RS='' -v ORS='' 'NR <= 2' since printing the record is the default action.
For a large file, you'd better quit after the second line.
gawk -v RS='' -v ORS='' 'NR==3 {exit} {print}'
Alternatively, you can use head. There's no option to use a null byte instead of a newline as the record separator, but you can swap the two characters, call head, and then swap back.
tr 'n' 'n' | head -n 2 | tr 'n' 'n'
answered Aug 6 '13 at 1:22
Gilles
529k12810611587
529k12810611587
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f85585%2fsplit-the-content-of-a-file-in-linux%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
are those the two characters
^and@? Or the NUL character (^@aka).– Stéphane Chazelas
Aug 5 '13 at 17:12
In such case my awk goes (I think): awk -F"x00" '{print $1"x00"$2"x00"}' abc.txt > newfile.txt
– GermanG
Aug 5 '13 at 18:12