-3

I use awk for print file line by line. I need to remove every newline character using awk.

My code

while($lines>0)
    echo -n "Output: '"
    awk NR==$i output.txt
    echo "'"
    @ i++
    @ lines--
end

Output must be like this: Output: 'something'

Maybe I need better solution

My file looks like this:
name1 10
name2 12
name3 5

My output must be:
Output: 'name1 10'
Output: 'name2 12'
Output: 'name3 5'

Thanks

Jakub Rosina
  • 59
  • 2
  • 7
  • I don't understand the requirement. Please post sample data with the related expected output. – James Brown Oct 24 '18 at 19:11
  • I need to have output like this: Output 'line from file' – Jakub Rosina Oct 24 '18 at 19:16
  • Do you want something like `tr -d '\n'< file` ? – Walter A Oct 24 '18 at 20:31
  • 1
    Sorry, but your code (and example data) make no sense. Based on you headline, just try `awk -v OFS=" " {print $0}' file`. Good luck. – shellter Oct 24 '18 at 20:59
  • This should work for all inputs: `awk 'BEGIN{print "Output \047line from file\047"; exit}'`. Good luck. – James Brown Oct 25 '18 at 04:43
  • @JamesBrown how to add line from file? – Jakub Rosina Oct 25 '18 at 10:56
  • @JamesBrown awk 'BEGIN{print "Output \047NR==$i\047"; exit}' prints **Output 'NR==$i'** – Jakub Rosina Oct 25 '18 at 11:49
  • 2
    your sample output is now further confusing the issue. Per your headline "Remove newline". your sample output has newlines in it. Also, do you really want it to include the word "Output:" ? AND do you really want the text from the file enclosed in single quotes (as shown in your example). Don't reply in comments, fix you Title Q and your sample input and output to be exactly what you are trying to solve. Good luck. – shellter Oct 25 '18 at 16:12
  • You might also want to clarify if you really need to use a shell looping construct to process your data. To produce your current sample output all that is needed is `awk '{print "Output: "'"'"'$0'"'"'}' file`. Good luck. – shellter Oct 25 '18 at 16:14

1 Answers1

1

... answering to example provided.

myfile:

$ cat myfile
name1 10
name2 12
name3 5

awk:

$ awk '{ print("Output: \047" $0 "\047"); }' myfile
Output: 'name1 10'
Output: 'name2 12'
Output: 'name3 5'

sed:

$ sed "s/.*/Output: '&'/" myfile
Output: 'name1 10'
Output: 'name2 12'
Output: 'name3 5'

bash:

$ while read -r LINE; do echo "Output: '${LINE}'"; done < myfile
Output: 'name1 10'
Output: 'name2 12'
Output: 'name3 5'
Kubator
  • 1,373
  • 4
  • 13