Issue
I have a file called sftp_output with the output of command from a sftp connection that list the content of some folders and look like this:
sftp> ls -l dir1/
-rw------- 1 200 100 1352 Jul 01 14:20 file1
-rw------- 1 200 100 1352 Jul 10 14:20 file2
sftp> ls -l dir2/
-rw------- 1 200 100 1352 Jul 01 14:20 file1
-rw------- 1 200 100 1352 Jul 10 14:20 file2
sftp> bye
What I need to do is filter all the files from dir1 to a single file called "dir1_contents"
and files from dir2 to a file called "dir2_contents"
. What is the best approach to do something like that?
The expected result needs to be something like this.
File: dir1_contents
file1
file2
What I need to do is filter all the files from dir1 to a single file called "dir1_contents"
and files from dir2 to a file called "dir2_contents"
. What is the best approach to do something like that?
I tried doing something like this
cat sftp_output | grep -v 'sftp' | awk '{print $9}'| sed '/^$/d'
Solution
This single awk
can handle this:
awk 'match($0,/ls +[^\/]+/) { # match a line that has ls command
s=substr($0,RSTART,RLENGTH)# get the matched substring into var s
sub(/^ls +/, "", s) # remove ls and 1+ spaces from s
close(fn) # close file handle, if open
fn = s "_contents" # populate variable fn
next # move to next line
}
fn { # if fn is not empty
print > fn # redirect current line to file `fn`
}' sftp_output
Answered By - anubhava Answer Checked By - David Goodson (WPSolving Volunteer)