2

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.

1
  • If you have vim you can do the following: qa0xjq This ways you have recorded a macro under "a" key, that moves to the beginning of the line (0), deletes one character (x) and moves to the lines below (j). To repeat this macro 100 times simply type 100@a But beware, that this will remove all first characters from the document (assuming that every line has a preceding space, that's what you wanted to happen). – Melon Feb 5 '14 at 20:39
5

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.

6
  • 1
    It will be great if you could explain all the parts of this command. – user68186 Feb 5 '14 at 20:25
  • It will remove one space following the start of the line. To remove more than one, use -e s/^ *// ;-) – Rmano Feb 5 '14 at 21:29
  • 1
    @Rmano Thats is correct however the OP asks to remove a single space. – jlliagre Feb 5 '14 at 21:34
  • Sorry for the slow response. We weren't given that command to use, but I will try it. If it works I'll use it. Thanks! – gbrooks Feb 10 '14 at 19:06
  • That worked perfectly, however is it possible to just use the command tr to do the same thing? – gbrooks Feb 10 '14 at 19:49
0
sed -e 's/^ //g' -e 's/,//g' file.txt

Explanation:

The first script (s/^ //g) is to replace all leading space with nothing (delete).
The second script (s/,//g) is to replace all commas with nothing (delete).

0

Bash has a way of using while IFS= read -r ; do . . . done < input.txt structure , to print exact contents of the file, but one of the peculiar features here is that if you omit IFS= , the leading spaces won't be read. Thus, we can do the following:

$ cat input.txt
 Text
$ while read -r line; do printf "%s\n" "$line"; done < input.txt                                                         
Text

And to replace the text of the original file, one can modify the command slightly like so:

$ cat input.txt
 Text
$ while read -r line; do printf "%s\n" "$line"; done < input.txt > temp.txt ; mv temp.txt input.txt                      
$ cat input.txt
Text

We can also use parameter expansion to delete commas as well

$ cat input.txt                                                                                                          
 Text, text,
$ while read -r line; do printf "%s\n" "${line//,}"; done < input.txt                                                    
Text text

tr -s" " could also be simulated via modifying printf slightly

$ cat input.txt                                                                                                          
 Text,     text,
$ while read -r line; do printf "%s " ${line//,};printf "\n"; done < input.txt                                           
Text text 

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.