Replace tabbed spaces in linux

How to replace spaces with tabs in linux in a text file?

+72
linux whitespace tabs
Sep 14 '09 at 21:57
source share
8 answers

Use the non-proliferation program (1)




UNEXPAND(1) User Commands UNEXPAND(1) NAME unexpand - convert spaces to tabs SYNOPSIS unexpand [OPTION]... [FILE]... DESCRIPTION Convert blanks in each FILE to tabs, writing to standard output. With no FILE, or when FILE is -, read standard input. Mandatory arguments to long options are mandatory for short options too. -a, --all convert all blanks, instead of just initial blanks --first-only convert only leading sequences of blanks (overrides -a) -t, --tabs=N have tabs N characters apart instead of 8 (enables -a) -t, --tabs=LIST use comma separated LIST of tab positions (enables -a) --help display this help and exit --version output version information and exit . . . STANDARDS The expand and unexpand utilities conform to IEEE Std 1003.1-2001 (``POSIX.1''). 
+137
Sep 14 '09 at 22:03
source share

I think you can try with awk

 awk -v OFS="\t" '$1=$1' file1 

or sed if you prefer

 sed 's/[:blank:]+/,/g' thefile.txt > the_modified_copy.txt 

or even tr

 tr -s '\t' < thefile.txt | tr '\t' ' ' > the_modified_copy.txt 

or a simplified version of tr-solution proposed by Sam Bisbee

 tr ' ' \\t < someFile > someFile 
+25
Sep 14 '09 at 10:00
source share

Using Perl :

 perl -p -i -e 's/ /\t/g' file.txt 
+7
Sep 14 '09 at 10:01
source share

better tr command :

 tr [:blank:] \\t 

This will clear the output, e.g. unzip -l , for further processing with grep, cut, etc.

eg.

 unzip -l some-jars-and-textfiles.zip | tr [:blank:] \\t | cut -f 5 | grep jar 
+6
Mar 24 2018-12-12T00:
source share

An example command to convert each .js file under the current director into tabs (only leading spaces are converted):

 find . -name "*.js" -exec bash -c 'unexpand -t 4 --first-only "$0" > /tmp/totabbuff && mv /tmp/totabbuff "$0"' {} \; 
+2
Nov 04 '15 at 9:24
source share

Download and run the following script to recursively convert soft tabs to hard tabs in text files.

Place and execute the script inside the folder containing the text files.

 #!/bin/bash find . -type f -and -not -path './.git/*' -exec grep -Iq . {} \; -and -print | while read -r file; do { echo "Converting... "$file""; data=$(unexpand --first-only -t 4 "$file"); rm "$file"; echo "$data" > "$file"; }; done; 
+2
Jan 31 '17 at 14:10
source share

You can also use astyle . I found it quite useful, and it has several options:

 Tab and Bracket Options: If no indentation option is set, the default option of 4 spaces will be used. Equivalent to -s4 --indent=spaces=4. If no brackets option is set, the brackets will not be changed. --indent=spaces, --indent=spaces=#, -s, -s# Indent using # spaces per indent. Between 1 to 20. Not specifying # will result in a default of 4 spaces per indent. --indent=tab, --indent=tab=#, -t, -t# Indent using tab characters, assuming that each tab is # spaces long. Between 1 and 20. Not specifying # will result in a default assumption of 4 spaces per tab.` 
+1
Aug 05
source share

This will replace consecutive spaces with a single space (but not a tab).

  tr -cs '[:space:]' 
0
Aug 14 '14 at 20:14
source share



All Articles