Tag Info

Hot answers tagged

17

echo print newline (\n) at end line echo abcd | xxd 0000000: 6162 6364 0a abcd. With some echo implementations, you can use -n: -n do not output the trailing newline and test: echo -n abcd | wc -c 4 With some others, you need the \c escape sequence: \c: Suppress the <newline> that otherwise follows the final argument in the ...


9

Simply: awk -v RS= -v OFS=, '{print $3,$6,$9,$12,$15,$18}' An empty record separator (RS=) enables the paragraph mode whereby records are separated by sequences of empty lines. Inside a record, the default field separator applies (records are separated by blanks) so in each record, the fields we are interested in are the 3rd, 6th, 9th... We change the ...


9

How about this command? csplit logname.log /---\ LOG\ REPORT\ ---/ {*} Testing cat logname.log --- LOG REPORT --- Mary Had A Little Lamb --- LOG REPORT --- Her Fleece Was White As Snow After running the above command, the output I get is, cat xx01 --- LOG REPORT --- Mary Had A Little Lamb cat xx02 --- LOG REPORT --- Her Fleece Was White As Snow


8

join -a1 -a2 -o 0,1.2,2.2 -e - file1.txt file2.txt


8

If you have only 4 lines after [part1] you can use -A4 option with grep: cat ${file} | grep -A4 "part1" | cut -d'=' -f2` For general case (more than 4 lines after [part1]) use sed to get the text between two parts: cat ${file} | sed -n "/part1/,/part2/p" | head -n-1 head is to delete additional part2 at the end As terdon said you don't have to use ...


7

You should be able to use sed -e :a -e '/\\$/N; s/\\\n//; ta' See Peter Krumins' Famous Sed One-Liners Explained, Part I, 39. Append a line to the next if it ends with a backslash "\".


7

You can for example use this: $ awk '/H/{sub("H", "H"++v)}1' file 1562 first part 1563 H1 col3 H col4 1564 H2 col3 H col4 3241 H3 col3 H col4 3242 third part ... This looks for those lines containing H and replaces that H with H together with a variable we keep incrementing. Note you could use gsub() instead of sub() if you wanted to perform ...


7

This is easy enough in awk using an array, split, and a regular loop: { split($2, elements, ",") out = "" for (i in elements) { el = elements[i] key = $1 " " el if (!(key in used)) { out = out el "," } used[key] = 1 } sub(/,$/, "", out) $2 = out } 1 For each line, we split the ...


6

Sed can't do arithmetic┬╣. Use awk instead. awk ' $4 == "calc" {sub(/calc( |\t)/, sprintf("%-6.2f", $3 - $2))} 1' The 1 at the end means to print everything (after any preceding transformation). Instead of the text substitution with sub, you could assign to $4, but doing so replaces inter-column space (which can be any sequence of spaces and tabs) ...


6

This sed command should do the trick. The following command will overwright the file: sed -i 's/^[^:]*:/:/' file To just print the output, remove the -i flag. To put the output in a new file, remove the -i flag and redirect the output: sed 's/^[^:]*:/:/' file > new_file


6

tac filename |awk '/man1/ { print $1,$2} '| awk '!x[$0]++' | tac Testing I wanted to test with more concrete input. So, my testing is as below. cat filename blah blah blah blah blahblah man1 boy1 blah blah man1 boy2 man1 boy1 man1 boy2 man1 boy3 man1 boy4 man1 boy2 Now, I run the above command and get the output as, tac filename |awk '/man1/ { print ...


6

You can use awk to remove some pipelines (processes) and to only read the file until encountering the first instance of Brightness: xrandr --verbose | awk '/Brightness/ { print $2; exit }'


6

How about something like awk '/--- LOG REPORT ---/ {n++;next} {print > "test"n".out"}' logname.log


5

You can get some of what you want via pipelog, which "allows for rotating or clearing the log of a running process by piping it through an intermediate which responds to external signals", e.g.: spewstuff | pipelog spew.log -p /tmp/spewpipe.pid -x "gzip spew.log.1" You can then get the pid from /tmp/spewpipe.pid, and: kill -s USR1 ...


5

This looks like a case where cut will do better than awk: cut -d , -f1-20 Precipitation.csv > aaa1 cut -d , -f21-40 Precipitation.csv > aaa2 cut -d , -f41-100 Precipitation.csv > aaa3 -d , specifies the delimiter (a comma, since the input is called CSV, but you can change that). -f N-M picks out fields N through M inclusive to be in the output. ...


5

sed -i 's|~/deleted|"$defaultpath"|g' file.sh Explanation: -i tells sed to modify the file in place. s|~/deleted|"$defaultpath"|g tells sed to replace ~/deleted with "$defaultpath" whereever it finds it. Extra feature: preserve ~/deleted on the third line You did not ask for this but, in your example script, it would be nice to leave ~/deleted ...


5

You appear to be using GNU sed or another version with the \u extension, so you can do this: sed -e '/gr-description/{n;s/\b./\u&/g;}' < test1 This matches lines containing gr-description, and then runs everything in the {} at that point. n goes to the next line, printing the one we just matched, and then the s command replaces all characters that ...


5

To delete lines that contain both .. and @: awk '!/@/ || !/\.\./' Or: sed -e '/@/!b' -e '/\.\./d' Or as suggested by @rush: sed '/@/{/\.\./d;}'


5

With GNU recode: recode html < file


5

With awk you could do it like this: $ awk 'NR==FNR{a[$1]=$2;next}{print $0, a[$1]}' file2 file1 COG0001 882.DVU3168 H COG0002 883.DvMF_2502 E COG0001 1140.Synpcc7942_0645 H COG0001 1148.SYNGTS_2220 H The NR==FNR{a[$1]=$2;next} block is run for file2, and it stores the second field in a map, using the first field as a key. The {print $0, a[$1]} block is ...


5

You can use sed: sed -e 's/[0-9]*=/=/' < data This replaces (s) any text that is zero or more characters in the range 0-9 followed by an = sign with just the = sign.


5

First of all, you should use a proper CSV parser. For example, in Perl, you can use Text::CSV: Install cpanm (if you use Perl, you'll thank me later) $ sudo apt-get install cpanminus If you're not on a Debian based system, you should be able to install it using your distribution's package manager. Install the Text::CSV module $ sudo cpanm Text::CSV ...


5

From man grep: -H, --with-filename Print the file name for each match. This is the default when there is more than one file to search. It will print the filename first, followed by the match; which isn't what you've shown in your example results. But it is quick and easy if that doesn't cause a problem. As it's default for ...


5

setopt extendedglob cat <->.csv > all.csv Where <-> matches any positive integer decimal number, will concatenate all those (in lexical order, which for 0 padded numbers is the same as numerical order) into all.csv. That will double the space on disk though. If you don't intend to keep the original files, you could do: for i in ...


5

In addition to @LatinSuD's suggestion of using grep's -m flag to stop reading after a match, you can adjust the size of xrandr's stdout buffer with a tool like stdbuf like so: BRIGHTNESS=`stdbuf -o0 xrandr --verbose | grep -m 1 -i brightness | cut -f2 -d ' '` This can give you a significant speed increase: $ cat brightness xrandr --verbose | grep -m 1 -i ...


5

Including the last line you'd do: sed -n '/word/,$p' That matches the first occurrence of word all the way until the last line and prints all matches. Not including the last line: sed '/word/,$!d;$d' ...which deletes negated matches and then deletes the last line. And to get from only the last match to the last line you have to try a little harder: ...


5

With awk: $ awk 'FNR%2==0{sub(255,1)}1' file myName_tx_1 VARCHAR(255) myName_in_1 VARCHAR(1) myName_tx_2 VARCHAR(255) myName_in_2 VARCHAR(1) myName_tx_3 VARCHAR(255) myName_in_3 VARCHAR(1) myAddress_tx_1 VARCHAR(255) myAddress_in_2 VARCHAR(1) Explanation FNR%2==0 only matches even lines. If line is even, we replace 255 with 1, sub(255,1). 1 is a true ...


5

With GNU sed you can specify line addresses in first~step format so to modify every other line starting with the second you could use address 2~2 e.g. $ sed '2~2 s/(255)/(1)/' file myName_tx_1 VARCHAR(255) myName_in_1 VARCHAR(1) myName_tx_2 VARCHAR(255) myName_in_2 VARCHAR(1) myName_tx_3 VARCHAR(255) myName_in_3 VARCHAR(1) myAddress_tx_1 VARCHAR(255) ...


5

You can pipe output to awk: $ ... | awk '/0\.1\.0/,/1\.0\.2/' 0.1.0 0.2.0 1.0.0 1.0.1 1.0.2


4

A bash solution: declare -a out EOF=false IFS=$'=' until $EOF do ...



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