Sed with filename from pipe

In the folder I have many files with several parameters in the file names, for example (with only one parameter) file_a1.0.txt, file_a1.2.txtetc.
They are generated using C ++ code, and I will need to take the last one (in time). I do not know a priori what the value of this parameter will be when the code is interrupted. After that I need to copy the second line of this last file.

To copy the second line of any file, I know this command sedworks:

sed -n 2p filename

I also know how to find the last generated file:

ls -rtl file_a*.txt | tail -1

Question:

How to combine these two operations? Of course, you can perform the second operation for this sed operation, but I do not know how to include the file name from the channel as input for this sed command.

+4
source share
4 answers

You can use this,

ls -rt1 file_a*.txt | tail -1 | xargs sed -n '2p'

(OR)

sed -n '2p' `ls -rt1 file_a*.txt | tail -1`

sed -n '2p' $(ls -rt1 file_a*.txt | tail -1)
+10
source

You can usually put a command in reverse ticks to put your output at a specific point in another command - like this

sed -n 2p `ls -rt name*.txt | tail -1 `

Alternatively - and preferable, because it is easier to invest, etc.

sed -n 2p $(ls -rt name*.txt | tail -1)
+3
source

-r in ls .

   -r, --reverse
          reverse order while sorting

, tail -1.

(head -1 r ls) , , , pipe to tail

sed -n 2p $(ls -t1 name*.txt | head -1 )
0

: grep grep sed. , , , , sed , :

, :

grep -i -l -r foo ./* 

, this_shell.sh( , script, this_shell.sh), , , , sed , foo :

grep -i -l -r --exclude "this_shell.sh" foo ./* | tee  /dev/fd/2 | while read -r x; do sed -b -i 's/foo/bar/gi' "$x"; done 

, , . grep ( , , )

. . (?)

fwiw - tail, , .

0
source

Source: https://habr.com/ru/post/1527509/


All Articles