how to paste part of the file name to the content of file?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
5
down vote

favorite












I have a folder with 1000 files; all characters before mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp refer to individual's ID (for example NA21117,NA21119,NA21126,..)



NA21117.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp
NA21119.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp
NA21126.mapped.ILLUMINA.bwa.GIH.low_coverage.20121211.bam_dp
NA21127.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp
NA21137.mapped.ILLUMINA.bwa.GIH.low_coverage.20120522.bam_dp
NA21142.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp
NA21143.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp


Each file has only one row:



cat NA21143.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp
1 115258827 10


for each of these files, I want to paste the individual ID to the content of file and get an out put like:



1 115258827 10 NA21143


Is there anyway to do it?







share|improve this question























    up vote
    5
    down vote

    favorite












    I have a folder with 1000 files; all characters before mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp refer to individual's ID (for example NA21117,NA21119,NA21126,..)



    NA21117.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp
    NA21119.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp
    NA21126.mapped.ILLUMINA.bwa.GIH.low_coverage.20121211.bam_dp
    NA21127.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp
    NA21137.mapped.ILLUMINA.bwa.GIH.low_coverage.20120522.bam_dp
    NA21142.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp
    NA21143.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp


    Each file has only one row:



    cat NA21143.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp
    1 115258827 10


    for each of these files, I want to paste the individual ID to the content of file and get an out put like:



    1 115258827 10 NA21143


    Is there anyway to do it?







    share|improve this question





















      up vote
      5
      down vote

      favorite









      up vote
      5
      down vote

      favorite











      I have a folder with 1000 files; all characters before mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp refer to individual's ID (for example NA21117,NA21119,NA21126,..)



      NA21117.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp
      NA21119.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp
      NA21126.mapped.ILLUMINA.bwa.GIH.low_coverage.20121211.bam_dp
      NA21127.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp
      NA21137.mapped.ILLUMINA.bwa.GIH.low_coverage.20120522.bam_dp
      NA21142.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp
      NA21143.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp


      Each file has only one row:



      cat NA21143.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp
      1 115258827 10


      for each of these files, I want to paste the individual ID to the content of file and get an out put like:



      1 115258827 10 NA21143


      Is there anyway to do it?







      share|improve this question











      I have a folder with 1000 files; all characters before mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp refer to individual's ID (for example NA21117,NA21119,NA21126,..)



      NA21117.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp
      NA21119.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp
      NA21126.mapped.ILLUMINA.bwa.GIH.low_coverage.20121211.bam_dp
      NA21127.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp
      NA21137.mapped.ILLUMINA.bwa.GIH.low_coverage.20120522.bam_dp
      NA21142.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp
      NA21143.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp


      Each file has only one row:



      cat NA21143.mapped.ILLUMINA.bwa.GIH.low_coverage.20130415.bam_dp
      1 115258827 10


      for each of these files, I want to paste the individual ID to the content of file and get an out put like:



      1 115258827 10 NA21143


      Is there anyway to do it?









      share|improve this question










      share|improve this question




      share|improve this question









      asked May 2 at 18:55









      Anna1364

      421110




      421110




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Use a loop:



          #!/bin/bash

          shopt -s nullglob
          for file in ???????.mapped.*bam_dp; do
          [[ -f "$file" ]] || continue
          id=$file%%.* # grab the ID from file name
          sed -i "s/$/ $id/" "$file" # modify the file in-place
          done





          share|improve this answer























          • Nice solution overall! It would be better to set shopt -s nullglob than to check for file existence with [[ -f "$file" ]].
            – David Foerster
            May 2 at 23:54











          • Should be noted that this assumes a 7-character ID. This might not necessarily be the case.
            – Bob
            May 3 at 3:05










          • @DavidFoerster: Thanks for pointing out nullglob. I modified the answer. [[ -f "$file" ]] is meant for skipping directories, if any.
            – codeforester
            May 3 at 3:10


















          up vote
          4
          down vote













          plain bash



          for file in *.bam_dp; do 
          contents=$(< "$file")
          echo "$contents $file%%.*" > "$file"
          done



          for multi-line files, still can be accomplished with plain bash



          for file in *.bam_dp; do 
          mapfile -t contents < "$file"
          printf "%sn" "$contents[@]/%/ $file%%.*" > "$file"
          done


          notes:



          • the mapfile command reads the file into an array of lines.

          • the $var/pattern/string parameter expansion does a search-and-replace on the variable value. (documented in the manual)

            • if pattern starts with % the pattern is anchored at the end of the string. Here, I'm matching the empty pattern at the end of the string.

            • the variable can be an array expansions, in which case the replacement occurs for each array element.


          Frankly, this approach is too clever, and I'd go for something more obvious.






          share|improve this answer























          • yes this works. I have additional question. I have some other files in another folder which have more than one row! I want to do exactly the sam but paste the individual ID to each row. Do you know how can I do this? Thanks a lot
            – Anna1364
            May 2 at 20:48











          • In that case, go for Ole's or codeforester's solutions, which will operate on every line in the files.
            – glenn jackman
            May 2 at 21:03

















          up vote
          2
          down vote













          Remove .* from $ARGV then append t $ARGV to the file:



          perl -i -pe '$ARGV=~s/..*//; s/$/t$ARGV/;' NA*


          Glenn's solution is most likely faster to run:



          perl -i -lpe '$_ .= " " . substr($ARGV,0,index($ARGV,"."))' NA*


          though if each file is only a single line, most of the time will be seeking on the drive.






          share|improve this answer























          • You might consider $_ .= "t$ARGV" instead of s///
            – glenn jackman
            May 2 at 19:53






          • 1




            or even perl -i -lpe '$_ .= " " . substr($ARGV,0,index($ARGV,"."))' NA*
            – glenn jackman
            May 2 at 19:56

















          up vote
          1
          down vote













          awk



          This method is compatible with the GNU (Linux) and BSD (Mac) versions of awk.



          awk ' id=FILENAME ; sub(/..*/,"",id) ; print $0 "t" id ' *.bam_dp



          • id=FILENAME ; sub(/..*/,"",id)
            Store the first part of each *.bam_dp filename (everything before the first .) as id.


          • print $0 "t" id
            Print each file's contents, then a tab character, then the record's id.

          This will print a list with lines as in your example:



          1 115258827 10 NA21143


          The original files will not be modified. You can save this output by, for example, adding > file.txt to the end of the command.






          share|improve this answer





















          • print $0, id will use the default field separator and is (arguably) nicer to read.
            – glenn jackman
            May 2 at 21:16










          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          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%2f441381%2fhow-to-paste-part-of-the-file-name-to-the-content-of-file%23new-answer', 'question_page');

          );

          Post as a guest






























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          Use a loop:



          #!/bin/bash

          shopt -s nullglob
          for file in ???????.mapped.*bam_dp; do
          [[ -f "$file" ]] || continue
          id=$file%%.* # grab the ID from file name
          sed -i "s/$/ $id/" "$file" # modify the file in-place
          done





          share|improve this answer























          • Nice solution overall! It would be better to set shopt -s nullglob than to check for file existence with [[ -f "$file" ]].
            – David Foerster
            May 2 at 23:54











          • Should be noted that this assumes a 7-character ID. This might not necessarily be the case.
            – Bob
            May 3 at 3:05










          • @DavidFoerster: Thanks for pointing out nullglob. I modified the answer. [[ -f "$file" ]] is meant for skipping directories, if any.
            – codeforester
            May 3 at 3:10















          up vote
          2
          down vote



          accepted










          Use a loop:



          #!/bin/bash

          shopt -s nullglob
          for file in ???????.mapped.*bam_dp; do
          [[ -f "$file" ]] || continue
          id=$file%%.* # grab the ID from file name
          sed -i "s/$/ $id/" "$file" # modify the file in-place
          done





          share|improve this answer























          • Nice solution overall! It would be better to set shopt -s nullglob than to check for file existence with [[ -f "$file" ]].
            – David Foerster
            May 2 at 23:54











          • Should be noted that this assumes a 7-character ID. This might not necessarily be the case.
            – Bob
            May 3 at 3:05










          • @DavidFoerster: Thanks for pointing out nullglob. I modified the answer. [[ -f "$file" ]] is meant for skipping directories, if any.
            – codeforester
            May 3 at 3:10













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Use a loop:



          #!/bin/bash

          shopt -s nullglob
          for file in ???????.mapped.*bam_dp; do
          [[ -f "$file" ]] || continue
          id=$file%%.* # grab the ID from file name
          sed -i "s/$/ $id/" "$file" # modify the file in-place
          done





          share|improve this answer















          Use a loop:



          #!/bin/bash

          shopt -s nullglob
          for file in ???????.mapped.*bam_dp; do
          [[ -f "$file" ]] || continue
          id=$file%%.* # grab the ID from file name
          sed -i "s/$/ $id/" "$file" # modify the file in-place
          done






          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited May 3 at 3:10


























          answered May 2 at 19:02









          codeforester

          335314




          335314











          • Nice solution overall! It would be better to set shopt -s nullglob than to check for file existence with [[ -f "$file" ]].
            – David Foerster
            May 2 at 23:54











          • Should be noted that this assumes a 7-character ID. This might not necessarily be the case.
            – Bob
            May 3 at 3:05










          • @DavidFoerster: Thanks for pointing out nullglob. I modified the answer. [[ -f "$file" ]] is meant for skipping directories, if any.
            – codeforester
            May 3 at 3:10

















          • Nice solution overall! It would be better to set shopt -s nullglob than to check for file existence with [[ -f "$file" ]].
            – David Foerster
            May 2 at 23:54











          • Should be noted that this assumes a 7-character ID. This might not necessarily be the case.
            – Bob
            May 3 at 3:05










          • @DavidFoerster: Thanks for pointing out nullglob. I modified the answer. [[ -f "$file" ]] is meant for skipping directories, if any.
            – codeforester
            May 3 at 3:10
















          Nice solution overall! It would be better to set shopt -s nullglob than to check for file existence with [[ -f "$file" ]].
          – David Foerster
          May 2 at 23:54





          Nice solution overall! It would be better to set shopt -s nullglob than to check for file existence with [[ -f "$file" ]].
          – David Foerster
          May 2 at 23:54













          Should be noted that this assumes a 7-character ID. This might not necessarily be the case.
          – Bob
          May 3 at 3:05




          Should be noted that this assumes a 7-character ID. This might not necessarily be the case.
          – Bob
          May 3 at 3:05












          @DavidFoerster: Thanks for pointing out nullglob. I modified the answer. [[ -f "$file" ]] is meant for skipping directories, if any.
          – codeforester
          May 3 at 3:10





          @DavidFoerster: Thanks for pointing out nullglob. I modified the answer. [[ -f "$file" ]] is meant for skipping directories, if any.
          – codeforester
          May 3 at 3:10













          up vote
          4
          down vote













          plain bash



          for file in *.bam_dp; do 
          contents=$(< "$file")
          echo "$contents $file%%.*" > "$file"
          done



          for multi-line files, still can be accomplished with plain bash



          for file in *.bam_dp; do 
          mapfile -t contents < "$file"
          printf "%sn" "$contents[@]/%/ $file%%.*" > "$file"
          done


          notes:



          • the mapfile command reads the file into an array of lines.

          • the $var/pattern/string parameter expansion does a search-and-replace on the variable value. (documented in the manual)

            • if pattern starts with % the pattern is anchored at the end of the string. Here, I'm matching the empty pattern at the end of the string.

            • the variable can be an array expansions, in which case the replacement occurs for each array element.


          Frankly, this approach is too clever, and I'd go for something more obvious.






          share|improve this answer























          • yes this works. I have additional question. I have some other files in another folder which have more than one row! I want to do exactly the sam but paste the individual ID to each row. Do you know how can I do this? Thanks a lot
            – Anna1364
            May 2 at 20:48











          • In that case, go for Ole's or codeforester's solutions, which will operate on every line in the files.
            – glenn jackman
            May 2 at 21:03














          up vote
          4
          down vote













          plain bash



          for file in *.bam_dp; do 
          contents=$(< "$file")
          echo "$contents $file%%.*" > "$file"
          done



          for multi-line files, still can be accomplished with plain bash



          for file in *.bam_dp; do 
          mapfile -t contents < "$file"
          printf "%sn" "$contents[@]/%/ $file%%.*" > "$file"
          done


          notes:



          • the mapfile command reads the file into an array of lines.

          • the $var/pattern/string parameter expansion does a search-and-replace on the variable value. (documented in the manual)

            • if pattern starts with % the pattern is anchored at the end of the string. Here, I'm matching the empty pattern at the end of the string.

            • the variable can be an array expansions, in which case the replacement occurs for each array element.


          Frankly, this approach is too clever, and I'd go for something more obvious.






          share|improve this answer























          • yes this works. I have additional question. I have some other files in another folder which have more than one row! I want to do exactly the sam but paste the individual ID to each row. Do you know how can I do this? Thanks a lot
            – Anna1364
            May 2 at 20:48











          • In that case, go for Ole's or codeforester's solutions, which will operate on every line in the files.
            – glenn jackman
            May 2 at 21:03












          up vote
          4
          down vote










          up vote
          4
          down vote









          plain bash



          for file in *.bam_dp; do 
          contents=$(< "$file")
          echo "$contents $file%%.*" > "$file"
          done



          for multi-line files, still can be accomplished with plain bash



          for file in *.bam_dp; do 
          mapfile -t contents < "$file"
          printf "%sn" "$contents[@]/%/ $file%%.*" > "$file"
          done


          notes:



          • the mapfile command reads the file into an array of lines.

          • the $var/pattern/string parameter expansion does a search-and-replace on the variable value. (documented in the manual)

            • if pattern starts with % the pattern is anchored at the end of the string. Here, I'm matching the empty pattern at the end of the string.

            • the variable can be an array expansions, in which case the replacement occurs for each array element.


          Frankly, this approach is too clever, and I'd go for something more obvious.






          share|improve this answer















          plain bash



          for file in *.bam_dp; do 
          contents=$(< "$file")
          echo "$contents $file%%.*" > "$file"
          done



          for multi-line files, still can be accomplished with plain bash



          for file in *.bam_dp; do 
          mapfile -t contents < "$file"
          printf "%sn" "$contents[@]/%/ $file%%.*" > "$file"
          done


          notes:



          • the mapfile command reads the file into an array of lines.

          • the $var/pattern/string parameter expansion does a search-and-replace on the variable value. (documented in the manual)

            • if pattern starts with % the pattern is anchored at the end of the string. Here, I'm matching the empty pattern at the end of the string.

            • the variable can be an array expansions, in which case the replacement occurs for each array element.


          Frankly, this approach is too clever, and I'd go for something more obvious.







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited May 3 at 6:12









          David Foerster

          915616




          915616











          answered May 2 at 19:51









          glenn jackman

          45.8k265100




          45.8k265100











          • yes this works. I have additional question. I have some other files in another folder which have more than one row! I want to do exactly the sam but paste the individual ID to each row. Do you know how can I do this? Thanks a lot
            – Anna1364
            May 2 at 20:48











          • In that case, go for Ole's or codeforester's solutions, which will operate on every line in the files.
            – glenn jackman
            May 2 at 21:03
















          • yes this works. I have additional question. I have some other files in another folder which have more than one row! I want to do exactly the sam but paste the individual ID to each row. Do you know how can I do this? Thanks a lot
            – Anna1364
            May 2 at 20:48











          • In that case, go for Ole's or codeforester's solutions, which will operate on every line in the files.
            – glenn jackman
            May 2 at 21:03















          yes this works. I have additional question. I have some other files in another folder which have more than one row! I want to do exactly the sam but paste the individual ID to each row. Do you know how can I do this? Thanks a lot
          – Anna1364
          May 2 at 20:48





          yes this works. I have additional question. I have some other files in another folder which have more than one row! I want to do exactly the sam but paste the individual ID to each row. Do you know how can I do this? Thanks a lot
          – Anna1364
          May 2 at 20:48













          In that case, go for Ole's or codeforester's solutions, which will operate on every line in the files.
          – glenn jackman
          May 2 at 21:03




          In that case, go for Ole's or codeforester's solutions, which will operate on every line in the files.
          – glenn jackman
          May 2 at 21:03










          up vote
          2
          down vote













          Remove .* from $ARGV then append t $ARGV to the file:



          perl -i -pe '$ARGV=~s/..*//; s/$/t$ARGV/;' NA*


          Glenn's solution is most likely faster to run:



          perl -i -lpe '$_ .= " " . substr($ARGV,0,index($ARGV,"."))' NA*


          though if each file is only a single line, most of the time will be seeking on the drive.






          share|improve this answer























          • You might consider $_ .= "t$ARGV" instead of s///
            – glenn jackman
            May 2 at 19:53






          • 1




            or even perl -i -lpe '$_ .= " " . substr($ARGV,0,index($ARGV,"."))' NA*
            – glenn jackman
            May 2 at 19:56














          up vote
          2
          down vote













          Remove .* from $ARGV then append t $ARGV to the file:



          perl -i -pe '$ARGV=~s/..*//; s/$/t$ARGV/;' NA*


          Glenn's solution is most likely faster to run:



          perl -i -lpe '$_ .= " " . substr($ARGV,0,index($ARGV,"."))' NA*


          though if each file is only a single line, most of the time will be seeking on the drive.






          share|improve this answer























          • You might consider $_ .= "t$ARGV" instead of s///
            – glenn jackman
            May 2 at 19:53






          • 1




            or even perl -i -lpe '$_ .= " " . substr($ARGV,0,index($ARGV,"."))' NA*
            – glenn jackman
            May 2 at 19:56












          up vote
          2
          down vote










          up vote
          2
          down vote









          Remove .* from $ARGV then append t $ARGV to the file:



          perl -i -pe '$ARGV=~s/..*//; s/$/t$ARGV/;' NA*


          Glenn's solution is most likely faster to run:



          perl -i -lpe '$_ .= " " . substr($ARGV,0,index($ARGV,"."))' NA*


          though if each file is only a single line, most of the time will be seeking on the drive.






          share|improve this answer















          Remove .* from $ARGV then append t $ARGV to the file:



          perl -i -pe '$ARGV=~s/..*//; s/$/t$ARGV/;' NA*


          Glenn's solution is most likely faster to run:



          perl -i -lpe '$_ .= " " . substr($ARGV,0,index($ARGV,"."))' NA*


          though if each file is only a single line, most of the time will be seeking on the drive.







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited May 2 at 20:49


























          answered May 2 at 19:13









          Ole Tange

          11.2k1343101




          11.2k1343101











          • You might consider $_ .= "t$ARGV" instead of s///
            – glenn jackman
            May 2 at 19:53






          • 1




            or even perl -i -lpe '$_ .= " " . substr($ARGV,0,index($ARGV,"."))' NA*
            – glenn jackman
            May 2 at 19:56
















          • You might consider $_ .= "t$ARGV" instead of s///
            – glenn jackman
            May 2 at 19:53






          • 1




            or even perl -i -lpe '$_ .= " " . substr($ARGV,0,index($ARGV,"."))' NA*
            – glenn jackman
            May 2 at 19:56















          You might consider $_ .= "t$ARGV" instead of s///
          – glenn jackman
          May 2 at 19:53




          You might consider $_ .= "t$ARGV" instead of s///
          – glenn jackman
          May 2 at 19:53




          1




          1




          or even perl -i -lpe '$_ .= " " . substr($ARGV,0,index($ARGV,"."))' NA*
          – glenn jackman
          May 2 at 19:56




          or even perl -i -lpe '$_ .= " " . substr($ARGV,0,index($ARGV,"."))' NA*
          – glenn jackman
          May 2 at 19:56










          up vote
          1
          down vote













          awk



          This method is compatible with the GNU (Linux) and BSD (Mac) versions of awk.



          awk ' id=FILENAME ; sub(/..*/,"",id) ; print $0 "t" id ' *.bam_dp



          • id=FILENAME ; sub(/..*/,"",id)
            Store the first part of each *.bam_dp filename (everything before the first .) as id.


          • print $0 "t" id
            Print each file's contents, then a tab character, then the record's id.

          This will print a list with lines as in your example:



          1 115258827 10 NA21143


          The original files will not be modified. You can save this output by, for example, adding > file.txt to the end of the command.






          share|improve this answer





















          • print $0, id will use the default field separator and is (arguably) nicer to read.
            – glenn jackman
            May 2 at 21:16














          up vote
          1
          down vote













          awk



          This method is compatible with the GNU (Linux) and BSD (Mac) versions of awk.



          awk ' id=FILENAME ; sub(/..*/,"",id) ; print $0 "t" id ' *.bam_dp



          • id=FILENAME ; sub(/..*/,"",id)
            Store the first part of each *.bam_dp filename (everything before the first .) as id.


          • print $0 "t" id
            Print each file's contents, then a tab character, then the record's id.

          This will print a list with lines as in your example:



          1 115258827 10 NA21143


          The original files will not be modified. You can save this output by, for example, adding > file.txt to the end of the command.






          share|improve this answer





















          • print $0, id will use the default field separator and is (arguably) nicer to read.
            – glenn jackman
            May 2 at 21:16












          up vote
          1
          down vote










          up vote
          1
          down vote









          awk



          This method is compatible with the GNU (Linux) and BSD (Mac) versions of awk.



          awk ' id=FILENAME ; sub(/..*/,"",id) ; print $0 "t" id ' *.bam_dp



          • id=FILENAME ; sub(/..*/,"",id)
            Store the first part of each *.bam_dp filename (everything before the first .) as id.


          • print $0 "t" id
            Print each file's contents, then a tab character, then the record's id.

          This will print a list with lines as in your example:



          1 115258827 10 NA21143


          The original files will not be modified. You can save this output by, for example, adding > file.txt to the end of the command.






          share|improve this answer













          awk



          This method is compatible with the GNU (Linux) and BSD (Mac) versions of awk.



          awk ' id=FILENAME ; sub(/..*/,"",id) ; print $0 "t" id ' *.bam_dp



          • id=FILENAME ; sub(/..*/,"",id)
            Store the first part of each *.bam_dp filename (everything before the first .) as id.


          • print $0 "t" id
            Print each file's contents, then a tab character, then the record's id.

          This will print a list with lines as in your example:



          1 115258827 10 NA21143


          The original files will not be modified. You can save this output by, for example, adding > file.txt to the end of the command.







          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered May 2 at 19:56









          Gaultheria

          3404




          3404











          • print $0, id will use the default field separator and is (arguably) nicer to read.
            – glenn jackman
            May 2 at 21:16
















          • print $0, id will use the default field separator and is (arguably) nicer to read.
            – glenn jackman
            May 2 at 21:16















          print $0, id will use the default field separator and is (arguably) nicer to read.
          – glenn jackman
          May 2 at 21:16




          print $0, id will use the default field separator and is (arguably) nicer to read.
          – glenn jackman
          May 2 at 21:16












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f441381%2fhow-to-paste-part-of-the-file-name-to-the-content-of-file%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?