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? I'm new to this so please help me with explanation.
Split the content of a file in linux
files;text processing
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 2abc.tarxxx.tarSo you needsed 's/\o000/\n/g' abc.txt | head -n 2 > newfile.txtExplanationThis 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 /\000/)[0..$num_lines-1];    print $_ for @a' abc.txt > newfile.txtReplace the value of $num_lines above as needed to grab the first $num_lines lines from the file.ExplanationThe -n switch tells perl to run the code on each line of the input fileThe -l000 sequence tells perl to set the output record separator(the character printed after every string) to the NUL byte (000).The -e switch tells perl that the string that follows is a code to execute.The split function 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 the split function (among others) when no argument is supplied.The final foreach loop prints every element in @a (again note how $_ is the default iterator for the foreach loop). Since we have set the output record separator to octal 000, we get the results separated by the NUL byte as before.