Monday, April 29, 2024
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 3930  / 3 Years ago, sun, july 25, 2021, 11:39:56

I have a text file that has a space before the line. How do I delete it using tr (or the correct command)?



For example, I have this:



 Text


I would like this:



Text   


But, how do I do that for a 200 line text file?



I currently have this pipeline:



cat file.txt | tr -s " " | tr -d "," 


The other tr commands are for removing other aspects of the text files.


More From » command-line

 Answers
1

This should do the job:



sed -e 's/^ //' -e 's/,//g' file.txt


The sed command (stream editor) is passed two commands to execute sequentially, both commands substitute something by nothing, i.e. delete a part of the input.



The first one removes spaces immediately following the beginning of a line, noted ^, the second one is removing the commas, and has the very same effect as your tr -d "," command.



Thanks to minerz029 for indirectly reminding me I was missing the 'g' as my first reply was only removing the first comma found in each line.


[#27117] Monday, July 26, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
imonove

Total Points: 82
Total Questions: 113
Total Answers: 106

Location: Saint Vincent and the Grenadines
Member since Wed, Nov 3, 2021
3 Years ago
;