Reputation: 231
I am a beginer in Shell script. Below is my requirement in UNIX Korn Shell.
When we list files using ls
command redirect to a file the file names will be stored as below.
$ ls FILE*>FLIST.TXT
$ cat FLIST.TXT
FILE1
FILE2
FILE3
But I need output as below with a prefixed constant string STR,:
$ cat FLIST.TXT
STR,FILE1
STR,FILE2
STR,FILE3
Please let me what should be the ls
command to acheive this output.
Upvotes: 8
Views: 14828
Reputation: 16039
The following should work:
ls FILE* | xargs -i echo "STR,{}" > FLIST.TXT
It takes every one of the file names filtered by ls
and adds the "STR,
" prefix to it prior to the appending
Upvotes: 8
Reputation: 37816
You can't use ls
alone to append data before each file. ls
exists to list files.
You will need to use other tools along side ls
.
You can append to the front of each line using the sed
command:
cat FLIST.TXT | sed 's/^/STR,/'
This will send the changes to stdout
.
If you'd like to change the actual file, run sed
in place:
sed -i -e 's/^/STR,/' FLIST.TXT
To do the append before writing to the file, pipe ls
into sed
:
ls FILE* | sed 's/^/STR,/' > FLIST.TXT
Upvotes: 13