0

I have a text file written in the following way :

/home/walter197/file_created.txt
/home/walter197/myfile1
/home/walter197/new.txt
/home/walter197/sdl.tar.gz
/home/walter197/sys_info.html
/home/walter197/test2.sh

I wrote the following shell script to read each line of the file text , cut the fourth column (which is the last column ) and write it to a new file:

#!/bin/bash 

path="/home/walter197/info.txt" 
new_file="/home/walter197/new_info.txt"

while IFS= read -r line
do 
    cut -d "/" -f 4 "$line" | cat $new_file

done < "$path"

exit

When i execute cat new_file.txt I got horrible weird texts .

So how can i fix the script so i can get the following in the new file:

file_created.txt
myfile1
new.txt 
sdl.tar.gz
sus_info.html
test2.sh 
1
  • 1
    You really don't need a shell loop here at all - just cut -d/ -f4 < "$path" > "$new_file" or cut -d/ -f4 "$path" > "$new_file" May 4, 2020 at 19:56

1 Answer 1

0

You have to change a couple of things:

#!/bin/bash 

path="/home/walter197/info.txt" 
new_file="/home/walter197/new_info.txt"

while IFS= read -r line
do 
    echo "$line" | cut -d "/" -f 4 >> $new_file

done < "$path"

exit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.