Split the content of a file in linux












5














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?










share|improve this question




















  • 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


















5














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?










share|improve this question




















  • 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
















5












5








5







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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












5 Answers
5






active

oldest

votes


















0














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 -n switch tells perl to run the code on each line of the input file

  • The -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.






share|improve this answer























  • 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



















3














Is this:



awk -F"@" '{print $1"@"$2"@"}' abc.txt > newfile.txt


good enough for you?






share|improve this answer























  • 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





















1














Try running:



sed -r -i 's/^(.*)@.*@.*$/1/' file





share|improve this answer























  • 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












  • 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










  • Done. I'm leaving the cat up to your discretion :)
    – Joseph R.
    Aug 5 '13 at 18:22



















0














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 file abc.txt a 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 @F and inserts a at sign (@) in between each column.






share|improve this answer





























    0














    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'





    share|improve this answer





















      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      0














      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 -n switch tells perl to run the code on each line of the input file

      • The -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.






      share|improve this answer























      • 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
















      0














      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 -n switch tells perl to run the code on each line of the input file

      • The -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.






      share|improve this answer























      • 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














      0












      0








      0






      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 -n switch tells perl to run the code on each line of the input file

      • The -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.






      share|improve this answer














      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 -n switch tells perl to run the code on each line of the input file

      • The -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.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      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


















      • 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













      3














      Is this:



      awk -F"@" '{print $1"@"$2"@"}' abc.txt > newfile.txt


      good enough for you?






      share|improve this answer























      • 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


















      3














      Is this:



      awk -F"@" '{print $1"@"$2"@"}' abc.txt > newfile.txt


      good enough for you?






      share|improve this answer























      • 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
















      3












      3








      3






      Is this:



      awk -F"@" '{print $1"@"$2"@"}' abc.txt > newfile.txt


      good enough for you?






      share|improve this answer














      Is this:



      awk -F"@" '{print $1"@"$2"@"}' abc.txt > newfile.txt


      good enough for you?







      share|improve this answer














      share|improve this answer



      share|improve this answer








      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




















      • 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













      1














      Try running:



      sed -r -i 's/^(.*)@.*@.*$/1/' file





      share|improve this answer























      • 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












      • 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










      • Done. I'm leaving the cat up to your discretion :)
        – Joseph R.
        Aug 5 '13 at 18:22
















      1














      Try running:



      sed -r -i 's/^(.*)@.*@.*$/1/' file





      share|improve this answer























      • 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












      • 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










      • Done. I'm leaving the cat up to your discretion :)
        – Joseph R.
        Aug 5 '13 at 18:22














      1












      1








      1






      Try running:



      sed -r -i 's/^(.*)@.*@.*$/1/' file





      share|improve this answer














      Try running:



      sed -r -i 's/^(.*)@.*@.*$/1/' file






      share|improve this answer














      share|improve this answer



      share|improve this answer








      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 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












      • 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










      • Done. I'm leaving the cat up 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












      • .* 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










      • 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
















      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











      0














      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 file abc.txt a 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 @F and inserts a at sign (@) in between each column.






      share|improve this answer


























        0














        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 file abc.txt a 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 @F and inserts a at sign (@) in between each column.






        share|improve this answer
























          0












          0








          0






          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 file abc.txt a 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 @F and inserts a at sign (@) in between each column.






          share|improve this answer












          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 file abc.txt a 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 @F and inserts a at sign (@) in between each column.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 5 '13 at 17:10









          slm

          247k66513678




          247k66513678























              0














              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'





              share|improve this answer


























                0














                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'





                share|improve this answer
























                  0












                  0








                  0






                  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'





                  share|improve this answer












                  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'






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 6 '13 at 1:22









                  Gilles

                  529k12810611587




                  529k12810611587






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Entries order in /etc/network/interfaces

                      新発田市

                      Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)