Skip to main content
Share Your Experience: Take the 2024 Developer Survey
65 votes

Bash shell script output alignment

Use printf to format output (it's also more portable than echo). I would also store the real value of the colour escape sequences instead of storing them in a form that requires expansion by echo. RED=...
Stéphane Chazelas's user avatar
58 votes

Bash shell script output alignment

Simply with column command: yourscript.sh | column -t The output: Network 10.x.xx.xxx : Online Network 10.x.xx.xxx : Offline Network 10.x.xx.xxx : Offline Network 10.x.xx.xxx ...
RomanPerekhrest's user avatar
48 votes

Purpose of “ASCII text, with overstriking” file format

Overstriking is a method used in nroff (see the Troff User’s Manual) to offer more typographical possibilities than plain ASCII would allow: bold text (by overstriking the same character) underlined ...
Stephen Kitt's user avatar
44 votes
Accepted

How do I convert a single column to 3 columns?

A solution with paste seq 10 | paste - - - 1 2 3 4 5 6 7 8 9 10 paste is a Unix standard tool, and the standard guarantees that this works for at least 12 ...
Paulo Tomé's user avatar
  • 3,772
43 votes
Accepted

How can I convert a text file to columns like a newspaper?

You can use fold to break the text up and then feed it to pr. Both are most likely available in your system. If this is the file lorem.txt: Lorem ipsum dolor sit amet, consectetur adipiscing elit, ...
Eduardo Trápani's user avatar
39 votes
Accepted

cat files in specific order based on number in filename

Use brace expansion cat file.88_{0..100}.pdb >>bigfile.pdb To ignoring printing the error messages for non-existent files use: cat file.88_{0..100}.pdb >>bigfile.pdb 2>/dev/null In ...
35 votes

How can I convert a text file to columns like a newspaper?

You can use fmt to format paragraphs to the desired column width followed by pr to arrange the columns. Both are POSIX utilities, although the GNU versions have more options than the POSIX minimum. I ...
Gilles 'SO- stop being evil''s user avatar
29 votes

How to list only files in a directory separated by commas

You can use tr for that job. ls -p | grep -v / | tr '\n' ','
Thomas's user avatar
  • 6,412
28 votes
Accepted

How does the file command distinguish text and LaTeX files?

The file type recognition is driven by so-called magic patterns. The magic file for analyzing TeX family source code contains a number of macro names that cause a file to be classified as LaTeX. Each ...
phg's user avatar
  • 1,817
25 votes
Accepted

How to use the 'date' command to display week number of the year?

The entire format string is to be preceded by the +: $ date +"So this is week: %U" So this is week: 19 $ date +"So this is week: %U of %Y" So this is week: 19 of 2016
muru's user avatar
  • 73.2k
25 votes
Accepted

Replacing underscore by comma and removing double quotes in CSV

Far simpler way is to use tr $ tr '_' ',' < input.csv | tr -d '"' 1,1,0,0,76 1,1,0,0,77 1,1,0,0,78 The way this works is that tr takes two arguments - set of characters to ...
Sergiy Kolodyazhnyy's user avatar
25 votes

How to do formatted printing with jq?

jq can reference expressions inside string using \(foo) String interpolation - \(foo) Inside a string, you can put an expression inside parens after a backslash. Whatever the expression returns will ...
dosmanak's user avatar
  • 358
24 votes

Formatting text into columns

If the input had been only two columns, I would have suggested to use column -t. This doesn't quite work here though since the column utility will treat any number of spaces or tabs as column ...
Kusalananda's user avatar
  • 337k
21 votes

AWK - print range of columns

The utility cut has a compact notation: cut -d, -f2-7 <input-file> producing: column2,column3,column4,column5,column6,column7 Answering the comment by @PlasmaBinturong: my intent was address ...
drl's user avatar
  • 838
20 votes
Accepted

Purpose of “ASCII text, with overstriking” file format

A web search for "backspace" and "overstrike" would get better results. The file is a manual page — formatted using nroff. Usually files such as bash.0 are simply generated and discarded. A ...
Thomas Dickey's user avatar
20 votes
Accepted

How to process an x-column text file to get a y-column one?

Put each field on a line and post-columnate. Each field on one line tr tr -s ' ' '\n' < infile grep grep -o '[[:alnum:]]*' infile sed sed 's/\s\+/\n/g' infile or more portable: sed 's/\s\+/...
Thor's user avatar
  • 17.2k
20 votes
Accepted

Add space before uppercase letter

Using sed, and assuming you don't want a space in front of the word: $ sed 's/\([^[:blank:]]\)\([[:upper:]]\)/\1 \2/g' file.in Add Data Test Something Tell Me Who You Are The substitution will look ...
Kusalananda's user avatar
  • 337k
20 votes

Formatted shell script output

An easy way: pipe your script to column -t $ script.sh | column -t a OK aa OK aaa OK aaaa OK aaaaaa ...
John Goofy's user avatar
19 votes

Formatted shell script output

Use the following printf approach to get the needed output: #!/bin/bash for i in a aa aaa aaaa aaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa; do printf '%-50sOK\n' $i done Script output: ...
RomanPerekhrest's user avatar
19 votes
Accepted

How to JSON-escape input?

jq -R -s '.' < datafile This reads in all of datafile as a string, and then has jq just print it out as a JSON string. It will give you a quoted string suitable for substituting into that template ...
Michael Homer's user avatar
19 votes
Accepted

How to do formatted printing with jq?

One way of doing it would be to not trying to do it in jq, and instead use jq to output a shell statement to do it in the shell instead: eval "$( jq -r -n ' { "title": "...
Kusalananda's user avatar
  • 337k
16 votes
Accepted

awk unintentionally removing commas

As pointed out by taliezin and pfnuesel, when defining the input file separator as a ',' it is necessary to also define the output file separator as a ',' to keep it. If the output file separator is ...
GustavMahler's user avatar
16 votes

How to do formatted printing with jq?

jq does not have printf. One way could be; Partially taken from here: echo '{"title" : "A sample name", "number" : 1214}' | jq '(.title) + " " + (.number |...
ibuprofen's user avatar
  • 2,920
15 votes
Accepted

increasing ps -o column width

In GNU/Linux you can set the column width as follows: ps -e -o cmd:50,stime,etime= From the ps(1) manual page: -o format User-defined format. format is a single argument in the form of a ...
zuazo's user avatar
  • 3,042
15 votes

How can I pretty format a JSON file, with all the correct indents and everything?

Use jq a very good JSON processor and from personal preference, its the best available in the market for just pretty print, use jq . file_name
dgfjxcv's user avatar
  • 707
15 votes
Accepted

Modify width of first column in file with a variable number of fields, using awk

You can use sprintf to re-format $1 only. Ex. $ awk 'BEGIN{OFS=FS="|"} {$1 = sprintf("%-3s",$1)} 1' file c1 |c2|c3|c4|c5 c6 |c7|c8|c9|c10 c11|c12|c13|c14|c15
steeldriver's user avatar
  • 81.5k
14 votes

Replace any number of tabs and spaces with single new line in Linux?

etopylight was almost right: tr -s ' \t' '\n' because the question asks to replace tabs, too.
G-Man Says 'Reinstate Monica''s user avatar
14 votes
Accepted

Understanding Sed command with extended RegExp

sed -E 's/.*"([^"]+)".*/\1/ -E: sed will use Extended Regex 's': to substitute value. /: the separator of pattern and replacement that will be use. .*"([^"]+)".*: the ...
Kiwy's user avatar
  • 9,575
13 votes

Replacing underscore by comma and removing double quotes in CSV

Just as an alternative, you can also use this sed command: $ sed -e 's/_/,/g' -e 's/"//g' input.csv 1,1,0,0,76 1,1,0,0,77 1,1,0,0,78
IanC's user avatar
  • 850
13 votes
Accepted

Rearrange file so it is easily readable

Self-counting version, season to taste: awk ' $1!=last {n=0;last=$1} {++n;gaggle[n]=gaggle[n]"\n"$0} END { for (k in gaggle) print gaggle[k] } '
jthill's user avatar
  • 2,720

Only top scored, non community-wiki answers of a minimum length are eligible