Tag Info

Hot answers tagged

18

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 ...


12

You can use -b to get the byte offset, which is the same as the position for simple text (but not for UTF-8 or similar). $ echo "RAMSITALSKHMAN|1223333" | grep -aob '|' 14:| In the above, I use the -a switch to tell grep to use the input as text; necessary when operating on binary files, and the -o switch to only output the matching character(s). If you ...


10

You can use sed: sed -i -e 'p' -e 's/o/0/g' file Explanation: -i: activates in-place editing of the file -e 'p': just pastes the line -e 's/o/0/g': replaces o with 0 and pastes the altered line And if you want an awk solution: awk '1;gsub("o", "0")' file >new_file


9

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 ...


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


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 ...


7

Try: printf '%s\n' 'RAMSITALSKHMAN|1223333.' | grep -o . | grep -n '|' output: 15:| This will give you the position with index based-1.


7

why not use awk ? I don't have a mac to test, but length() is a pretty standard function in awk, so this should work. awk file: { for (i=1;i<=NF;i++) { l=length($i) ; if ( l > linesize[i] ) linesize[i]=l ; } } END { for (l in linesize) printf "Columen%d: %d\n",l,linesize[l] ; } then run mybox$ awk -F, -f test.awk a.txt Columen4: 4 ...


6

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) ...


6

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 ...


6

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


6

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


6

chaos' answer is good if every line matches. But if some lines don't contain a o, they'll be duplicated. For example: $ echo -e 'foo\nbar' | sed -e 'p' -e 's/o/0/g' foo f00 bar bar   Here are some other solutions which don't duplicate lines: $ echo -e 'foo\nbar' | sed -e '/o/ p; s//0/g' foo f00 bar   $ echo -e 'foo\nbar' | perl -pe '/o/ ...


6

This is not answering your question, but it solves the problem you are trying to solve, in a completely different way: The full command is this (see below for example output): ps -o comm,%cpu,%mem --sort -%cpu -A | head -6 I will describe the parts of it: using ps to have more control about the output Printing only the three columns we need with -o ...


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

you can pipe it to sed: .... | sed -n "/0.1.0/,/1.0.2/p" YourFile


5

You can use sed to remove the rest - everything starting with the last slash: mpc -f %file% | head -1 | sed 's:/[^/]*$::' The pattern /[^/]*$ matches a slash, followed by any characters except slash, up to the end of the line. It is replaced by the empty string. The head -1 ignores some status output following the line we want - but see below for how to ...


5

top command | awk 'BEGIN { print header; count=5} NR>=8 { if ($0 ~ /your top regex/) next; print fields; if (--count == 0) exit}' On every line starting with #8, if it matches top, ignore it. Otherwise, print the portion of it that you want. The fifth time you print a line (that doesn’t match top), exit.


5

With awk: $ awk '$2 = sprintf("%05.2f",$2)' file num 00.12 num num num 25.53 num num num 07.82 num num


5

Try: $ awk 'FNR==NR{a[$1]=$2;next}{$1 = $1"\t"a[$1]}1' OFS='\t' file1 file2 1 today alot 1 today sometimes 2 tomorrow at work 2 tomorrow at home 2 tomorrow sometimes 3 red new


4

If you run echo without the -n option, it writes a newline character after the arguments.  Otherwise, if you typed echo foo, your next shell prompt would appear to the right of the foo.  So wc is counting the newline. How can I prevent echo from printing that? echo -n abcd | wc -c


4

You need to use a more sophisticated tool to parse the file. For example, awk: #!/bin/sh getCalibDate() { file="${1}" value=$(awk '/\[part/{a++}(a<2 && /Val/){print $NF}' ${file}) for v in $value do echo $v done } getCalibDate ${1} Here, the variable a is incremented each time a line matches [part. Then, the last ...


4

The shell has a built-in variable expansion field separator. So if you have a string and your delimiter is solid you can do: var=32768,'dff0207a-591f-4435-9f8b-7b9b3e6ba2c1','d1f77359b3f7236806489ba3108c771f','NUMBER','US_EN','LATIN','GREEK','GERMAN' ( IFS=,; set -f for field in $var do printf '\n%s\n\t' "$field - md5:" >&2 printf ...


4

Try this: find /path/to/file/ -maxdepth 1 -type f -name '*.csv' -print0 | while read -d '' -r file; do sed -n '3,53p' $file; done Notice print0 option which take care of any possible whitespace characters in the file names.


4

Simply paste: paste -d'\n' file1.txt file2.txt If you want to use awk, you can: awk '{getline a < "file2.txt";printf "%s\n%s\n", $0, a}' file1.txt


4

Assuming that your logfile is called logfile, here is an awk solution with the sample output: $ awk '/RINGING/,/CLOSE/ {if (/30 30/){f=1}; a=a"\n"$0} f==0 && /CLOSE/ {print a} /CLOSE/{a="";f=0}' logfile 313782 Aug 19 18:37:04.925: <DATA> RINGING|254|01136097645|5950|$hostIp|$size |$data 313783 Aug 19 18:37:05.262: <DATA> ...


4

Do you specifically require an awk solution?  join file1 file2 > file3 will do exactly what you want.


4

A job for perl: perl -pe 's/\bname\b\K/"_".++$n/ge'


3

echo always outputs to its stdout1. echo foo always does a write(1, "foo\n"). When you do echo foo > file, the shell changes the stdout for the echo command to a new file file. echo foo then still does a write(1, "foo\n") but this time its stdout (its file descriptor 1) points to file, not to the stdout it inherits from the shell. If you want to write ...


3

There's sed '/_in_/ s/255/1/' which means: for lines matching /_in_/, search for 255 and replace with 1.



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