bash - for loop output formatting: add newline with a description and space -
i'm running through list of files cat , redirecting of output single file using loop. loop works i'm looking way add descriptor line before each file's contents dumped , add space before each new file entry. here script.
#!/bin/bash files=$(ls -l /home/user/*.txt | awk 'nr>=2 {print $9}') in $files; /bin/cat "$i" >> "/home/user/catfiles.txt" done
my output looks this:
spawn ssh user@x.x.x.x run $command quit spawn ssh user@x.x.x.x run $command quit
i this:
"description first file here" spawn ssh user@x.x.x.x run $command quit <space> "description second file here" spawn ssh user@x.x.x.x run $command quit <space>
update: file description name need vary file using actual file name.
"this $file1" "this $file2" etc,etc..
this merge them require it:
for f in /home/user/*.txt;do echo "this ${f##*/}" >> /home/user/catfiles.txt /bin/cat "${f}" >> /home/user/catfiles.txt echo >> /home/user/catfiles.txt done
the file name printed without path. if want path printed, too, replace ${f##*/}
${f}
.
update
${variable##pattern}
called parameter substitution. bash search pattern
in variable
, remove longest match. in case variable f
, pattern */
matches string ends slash. double hash ##
indicates remove longest string in f
can matched */
. since f
path match , remove , including last /
, leaving filename. bash documentation has further , more detailed info on subject. alternatively, can search in man bash
parameter expansion
.
Comments
Post a Comment