Tag Info

Hot answers tagged

47

Solving this by deleting every second line can be error prone (for example, when process sometimes generates two meaningful lines instead one). May be it is better to filter out the garbage: grep -v "No error occurred" file It can run as filter, you can add more garbage patterns here and improve the result.


18

With sed: sed -e n\;d <file With POSIX awk: awk 'FNR%2' <file If you have older awk (like oawk), you need: oawk 'NR%2 == 1' <file With ex: $ ex file <<\EX :g/$/+d :wq! EX will edit the file in-place. g mark a global command /$/ match every lines +d delete the next line wq! save all changes This approach share the same ideal ...


14

Suppose that we have this test file: $ cat file the cat in the hat the quick brown dog jack splat With grep implementations that have adopted GNU's -o extension, we can retrieve all the words containing a: $ grep -wo '[[:alnum:]]*a[[:alnum:]]*' file cat hat jack splat We can count those words: $ grep -wo '[[:alnum:]]*a[[:alnum:]]*' file | wc -l 4


9

Ассоrding to the question, with GNU sed: sed '0~2d' file will delete every second line but I'd like to offer filter lines by it content: sed '/Data/! d' file or with same result sed '/No error/d' file


8

POSIXly: <file tr -s '[:space:]' '[\n*]' | grep -c a Here, words are sequences of non-spacing characters.


7

I don't see why you would want to do it in a single awk command, what you have seems perfectly fine. Anyway, here's one way: $ awk -F, '(max[$18]<$21 || max[$18]==""){max[$18]=$21;line[$18]=$0} END{for(key in line){print line[key]}}' file 6598,6598,0,1,,1,0,1,1,0,0,0,1,0,0,0,0,1390,1390,,0.730000, ...


5

The general approach with this kind of task is to use awk or perl... to compute the metric you're interested in and prepend it to the line, and then feed that to sort and remove the metric off the sorted output: awk '{print gsub("a","a"), $0}' < file | sort -rn | cut -d' ' -f2-


5

Here's a Perl way: perl -0lnE 'say scalar grep(/a/,split(/\s/,$_));' file And an awk way: awk '{for(i=1;i<=NF;i++){if($(i)~/a/){k++}}}END{print k}' file


4

With ksh93, bash, zsh, mksh, recent FreeBSD sh: sed $'/\f/,/./{/\f/d;/./!d;}' That will fail if there are consecutive sequences of \f\n\n...s though. With GNU sed, you can omit the $. POSIXly ($'...' is not POSIX yet, though will probably be in SUSv5): FF=$(printf '\f') sed "/$FF/,/./{/$FF/d;/./!d;}" Note that the current POSIX spec requires a ...


4

That's the caret notation for the form feed character. With the GNU implementation of sed, you can remove it using its octal value, \o14 : sed 's/\o14//g' file You can also use its escape code: sed 's/\f//' file Such characters can be entered in the terminal by pressing CtrlV and then the code for the character. In this case, CtrlL. So, type this: ...


4

POSIXly: n=2 awk -F, '++a[$1]==ENVIRON["n"]' <file Occurrence of 1st field was track by increasing each time it was seen, saving in array a. If it's 2nd occurrence, the condition became true, making awk to print $0.


4

Here is a way using sed: sed -n 'p;n' filename Another way with GNU sed: sed -n '1~2p' filename Output from above commands: Data inserted into table. Total count 13 Data inserted into table. Total count 45 Data inserted into table. Total count 14 Data inserted into table. Total count 90


3

You can try with awk: awk 'NR % 2 != 0' file or you can print only lines containing Data inserted: awk '$0 ~ /Data inserted/' file


3

Another Schwartzian transform: $ awk -Fa '{print NF,$0}' file | sort -nr | cut -d' ' -f2- a man a plan a canal panama aardvark baseball cat bat bill Or, in Perl: perl -Fa -lane 'print "$#F $_"' file | sort -nr | cut -d' ' -f2-


3

You can also just sort on the character: tr -cd a\\n <file | paste - ./file | LC_ALL=C sort -rk1,1 | cut -f2- Here's what your example looks like after being translated and pasted before it is piped into sort: aa baseball aaa aardvark aaaaaaaaaa a man a plan a canal panama a cat a bat bill Then sort gets it and, all things being equal, ...


3

POSIXly you can get the decimal value of a hex number like: hex=10 echo "$((0x$hex))" 16 And you really don't need to do all of those [ tests ]. I think the following should work: case $f7 in (EnHr|EnSt|SpJb|Chem|[BT]rTm|PmTm|HyTm) printf "\t\t\t\t\t%s%b" \ "HEX VALUE is" ":\t$f10" \ '' "inside case loop.\t\t" \ Sen ...


3

Using uniq instead awk can be quicker a little: sort -t, -k18,18nr -k21,21nr | uniq -s39 -w4


3

With a BSD/GNU sed: find "/dir/folder" -name "*.dat" -exec sed -se1d {} + >> "/dir/folder/table.txt" ...which instructs to treat all input files separately, and for each to delete the 1st line. If the header isn't already in table.txt, you should first put it there, though: set -- /dir/folder/*.dat head -n1 <"$1" >>/dir/folder/table.txt ...


2

With sed: sed '/^$/{$!{N;/\n$/D;s/.//;$!h;$p;d};};//!{H;1h;$!d};$x' infile this should print the last set of non-empty lines without any leading/trailing empty lines. e.g. iostat -d 1 2 | sed '/^$/{ # if the line is empty $!{ # and if it's not the last line N # then pull in the next line ...


2

Another answer, you could use vi/vim! qdjddq And then if your file was 500 lines (for example) type 250@d And then to write and exit type :x Or if something goes wrong and you don't want to save: :q! Explanation: q #Start Recording d #Put the recording into register 'd' j #Move the cursor down dd #Delete the line ...


2

If the header is on one line, with GNU tail: find "/dir/folder" -name "*.dat" -exec tail -qn +2 {} + POSIXly, you'd need to run one tail per file: To preserve the header of the first file, GNUly: find "/dir/folder" -name "*.dat" -print0 | { IFS= read -rd '' first && cat "$first" && xargs -r0 tail -qn +2 }


2

awk can do that all alone: df -B KB | awk 'NR!=1&&$1!~/tmpfs|cdrom/{printf "'$(date "+%Y-%m-%d-%H:%M:%S")','$(hostname)',%s,%s,%s,%s\n", $2, $3, $4, $1}' Explanation: df -B KB: prints the values in KB awk NR!=1: avoid the first line $1!~/tmpfs|cdrom/: if the first field contains not the excluded filsystems printf: print formatted $(date ...


1

With GNU grep: grep -oP '\b\w*a+\w*\b' file


1

awk 'BEGIN{RS="[[:space:][:punct:]]"; c=0} index($0,"a"){c++} END{print c}' Using a version of awk that supports multi-character Record Separator (RS), eg. GNU awk, you can cause awk to read one word per record. Within that record, the index(in, string) function searches in for the first occurrence of string, and returns the 1-based character ...


1

It looks like what you want is to i) append the contents of file1 to file2 and ii) change the format of file2 to match that of file one. That is not what your question is actually asking for but it's what you show in your output. If I misunderstood, please edit your question and clarify. So, to do that, you could simply do: awk 'NR>1{printf ...


1

try awk 'FNR == NR { print ; next ; } NR > FNR && FNR > 1 { split($0,A) ; printf ",%s,,,,%s,,,\n",A[1],A[2] ; }' f1 f2 (can be in one line, I break it to be more readable) where FNR == NR { print ; next ; } copy lines from first file NR > FNR && FNR > 1 select line from second file without ...


1

sed "$(printf '$!N;/\f')"'\n/,/\n./!P;D' <in >out ...should work provided a POSIX compliant shell, printf, and sed. You don't really need printf though if you just insert the character literally. You can usually do it with CTRL+V then CTRL+L. So... sed '$!N;/^L\n/,/\n./!P;D' <in >out ...where the ^L sequence above is got by doing CTRL+V ...


1

I would think the quickest way to do it would look something like... sed -e's/./& /g;i\' -e'<item>' \ -ea\\ -e'<tag><out>=' <file | paste -d'\0 ""' - - - ./file /dev/null This also works: ( set -- - - - - - - /tmp/file paste -d'<item> <tag><out>="' "$@" - - - - - - "$@" | sed 's/ ...


1

You can do it without loop in awk: awk '{a=$1;gsub(/./,"& ",$1); print "<item>"$1"<tag><out>=""\""a"\""}' numbers.txt Output: <item>9 3 7 4 5 4 1 6 3 2 5 5 3 <tag><out>="9374541632553" <item>5 1 2 4 3 7 4 7 8 7 9 8 4 1 <tag><out>="51243747879841" <item>3 2 0 3 0 0 9 8 8 9 6 9 1 4 ...


1

Here's one way you might be able to do the whole thing in sed: sed ' h; s/./& /g; s/.*/<item>&<tag>out=/; G; s/\n\([0-9]*\)/"\1"/; ' numbers.txt



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