Hot answers tagged

156

If we use tr command along with squeeze option (-s flag ) to convert all multiple consecutive spaces to a single space and then perform cut operation with space as delimiter – we can access the required column carrying the numbers. Refer to the code snipped bellow: cat file | tr -s ' ' | cut -d ' ' -f 8


56

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=$(tput setaf 1) GREEN=$(tput setaf 2) YELLOW=$(tput setaf 3) NC=$(tput sgr0) online="${GREEN}online$NC" offline="${RED}offline$NC" ping -c 1 "$hostname" >...


48

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 : Offline Network 10.x.xx.x : Online Network 139.xxx.x.x : Online Network 208.xx.xxx.xxx : Online Network 193.xxx.xxx.x : Online


27

To answer your question literally: sed 's/ */:/g' | cut -d : -f 5 or awk -F ' +' '{print $5}' But that won't do if the number in brackets reaches 10, etc. If you're only interested in the numbers, you could remove everything else. sed 's/[^.0-9][^.0-9]*/:/g' | cut -d : -f 6


23

awk -F, '{sum+=length($3)}; END {print +sum}' file


23

cut -d, -f3 | tr -d '\n' | wc -m (remember that wc -c counts bytes, not characters: $ echo a,1,españa,2 | cut -d, -f3 | tr -d '\n' | wc -c 7 $ echo a,1,españa,2 | cut -d, -f3 | tr -d '\n' | wc -m 6 )


21

NOTE: Assuming you have this version of top (procps). You can check with this command: $ top --version top: procps version 3.2.8 usage: top -hv | -bcisSH -d delay -n iterations [-u user | -U user] -p pid [,pid ...] procps is often the version of top included with Fedora/CentOS/RHEL and other variants. Changing columns If you look in the man page for ...


20

There's a dedicated tool for that: paste. It concatenates each full line from the first file with the corresponding line from the second file; you can remove unwanted columns before or after. For example, assuming that your columns are tab-delimited: paste file1.txt file2.txt | cut -f 1,2,3,6 Here's a way to pre-filter both files that relies on ksh/bash/...


20

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 delimiters: $ column -t file1 John 1 Peter 2 Michael Rod 3 Su 7 Louise 9 "Michael Rod" is two columns, so that single row ...


19

Assuming you don't have any tab characters in your files, paste file1 file2 | expand -t 13 with the arg to -t suitably chosen to cover the desired max line width in file1. OP has added a more flexible solution: I did this so it works without the magic number 13: paste file1 file2 | expand -t $(( $(wc -L <file1) + 2 )) It's not easy to type but can ...


19

It is easiest with awk which treats multiple consecutive spaces as a single one, so awk '{print $2}' file prints 207 412 431 But obviously there are many, many other tools which will do the job, even not designed to such task as grep: grep -Po '^[^ ]+[ ]+\K[^ ]+' file


17

Have a look at GNU datamash which can be used like datamash transpose. A future version will also support cross tabulation (pivot tables)


17

If you have root permissions on that machine you can temporarily increase the "maximum number of open file descriptors" limit: ulimit -Hn 10240 # The hard limit ulimit -Sn 10240 # The soft limit And then paste res.* >final.res After that you can set it back to the original values. A second solution, if you cannot change the limit: for f in res.*; do ...


16

awk '{s+=$3}END{print s}' yourfile


16

Assuming a CSV file, you can use column(1) like so: column -ts, your_file This is included in the bsdmainutils package on my Debian distribution, so I'm not sure how portable it is. Two more things to note: The above example is simplistic; explore the man page for more details on how to format your output. It does not fare well with quoted fields ...


16

Use pipes to squeeze the extra whitespaces and send your data (e.g, in columns.txt) into cut: tr -s ' ' < columns.txt | cut -d" " -f2 In the example data you provided, a single space delimiter puts the data you want in field 5. However, if the first column was numerical and had leading spaces in order to align it to the right, you will need to ...


15

To columnate the output, pr converts text files for printing: -COLUMN, --columns=COLUMN: output COLUMN columns and print columns down, unless -a is used. -t, --omit-header : omit page headers and trailers e.g. ls /etc | pr -2 -t abrt gtk-2.0 acpi hal adjtime ...


14

Without doing some fancy RegEx where you count commas, you're better off using awk for this problem. awk -F, '$2=="ST"' The -F, parameter specifies the delimiter, which is set to a comma for your data. $2 refers to the second column, which is what you want to match on. "ST" is the value you want to match.


13

I'm not sure why ls -hal / | awk '{print $5, $9}' seems to you to be much more disruptive to your thought processes than ls -hal / | cut -d'\s' -f5,9 would have been, had it worked. Would you really have to write that down? It only takes a few awk lines before adding the {} becomes automatic. (For me the hardest issue is remembering which field number ...


13

Well, the first field is $1, NF holds the number of fields on the line, we can access the fields with $i where i is a variable, and loops work almost like in C. So: $ awk '{for (i = 2; i <= NF; i++) printf "%s\t%s\n", $1, $i} ' < blah 1 the 1 mouse ... (This doesn't differentiate between space and tab as field separator.)


11

You almost have it already: awk -F'\t' 'NF==13 {print}' infile > newfile And, if you're on one of those systems where you're charged by the keystroke ( :) ) you can shorten that to awk -F'\t' 'NF==13' infile > newfile To do multiple files in one sweep, and to actually change the files (and not just create new files), identify a filename thats not ...


11

I think you're looking for expand and/or unexpand. It seems you're trying to ensure a \tab width counts as 8 chars rather than the single one. fold will do that as well, but it will wrap its input to the next line rather than truncating it. I think you want: expand < input | cut -c -80 expand and unexpand are both POSIX specified: The expand utility ...


10

If chaos' answer isn't applicable (because you don't have the required permissions), you can batch up the paste calls as follows: ls -1 res.* | split -l 1000 -d - lists for list in lists*; do paste $(cat $list) > merge${list##lists}; done paste merge* > final.res This lists the files 1000 at a time in files named lists00, lists01 etc., then pastes ...


10

That's what join does: join -2 2 -a 1 pbas.txt s2.txt The options say: -2 2: the second file uses the second column to store the key -a 1: output all lines from file 1, even if there's no match in file 2.


10

You could re-print the first column, left-justified in a suitably wide field: $ awk '{$1 = sprintf("%-30s", $1)} 1' file super+t sticky toggle super+Shift+space floating toggle super+Shift+r restart super+Shift+d mode $mode_launcher super+Shift+c reload super+r ...


9

tr ' ' '\t' < file 1<> file Would replace every space character with a tab character. Just to respond to people saying it's not safe: The shell will open the file for reading on file descriptor 0, and for reading-and-writing on file descriptor 1. If any of those fail, it will bail out, tr won't even be executed. If the redirections are ...


9

Outside of rolling a custom solution to transpose rows with columns from a command line the only tool I've ever seen that can do this is a tool called ironically transpose. Installation Unfortunately it's not in any repo so you'll need to download and compile it. This is pretty straightforward since it has no additional libraries that it's dependent on. ...


9

#!/bin/bash day=2000-01-01 end=2012-12-31 echo Date > output.file until [[ $day > $end ]]; do printf "$day %d\n" $(seq 100 100 2400) day=$(date -d "$day + 1 day" +"%Y-%m-%d") done >> output.file


9

The LINE parameter isn't quoted so wordsplitting happens upon the expansion of $LINE in echo $LINE and by the time awk receives any input, you have 7 words(as seen by the shell) all separated by a single space. You want echo to output it as one word(again, as seen by the shell) so the whitespace in your line isn't mangled before awk can process it. That is ...


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