Given a text file with multiple lines, I would like to iterate over each line in a Bash script. I had attempted to use cut
, but cut
does not accept \n
(newline) as a delimiter.
This is an example of the file I am working with:
one two three four
Does anyone know how I can loop through each line of this text file in Bash?
Simply setting the IFS variable to a new line works for the output of a command but not when processing a variable that contains new lines. As you can see, echoing the variable or iterating over the cat command prints each of the lines one by one correctly.
The syntax to loop through each file individually in a loop is: create a variable (f for file, for example). Then define the data set you want the variable to cycle through. In this case, cycle through all files in the current directory using the * wildcard character (the * wildcard matches everything).
Execute the following command to insert the file's name, followed by a newline, followed by the text Loops Rule! into each file: for FILE in *; do echo -e "$FILE\nLoops Rule\!" > $FILE; done.
I found myself in the same problem, this works for me:
cat file.cut | cut -d$'\n' -f1
Or:
cut -d$'\n' -f1 file.cut
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With