Tag Info

Hot answers tagged

17

Using paste: paste -d \\n file2 file1


12

Using GNU sed: sed -i '1d;$d' Element_query How it works : -i option edit the file itself. You could also remove that option and redirect the output to a new file or another command if you want. 1d deletes the first line (1 to only act on the first line, d to delete it) $d deletes the last line ($ to only act on the last line, d to delete it) Going ...


12

You can use \u in GNU sed to uppercase a letter: sed -e 's/_\(.\)/_\u\1/' input Perl does the same: perl -pe 's/_(.)/_\u$1/' input \l does the opposite.


10

With GNU sed: sed -E 's/[[:alpha:]]+/\u&/3' Would capitalise the third sequence of letters from each line. To capitalise every third sequence of letters in each line: sed -E 's/(([[:alpha:]]+[^[:alpha:]]+){2})([[:alpha:]]+)/\1\u\3/g' To capitalise every third sequence of letters in the whole input, with GNU awk: awk -v RS='[^[:alpha:]]+' -v ORS= ...


10

Use single quotes for the expression you used: sed 's/\//\\\//g' In double quotes, \ has a special meaning, so you have to backslash it: sed "s/\//\\\\\//g" But it's cleaner to change the delimiter: sed 's=/=\\/=g' sed "s=/=\\\/=g"


7

That's the job for sed: sed -e 'G;G;G;G;G' file With awk: nawk -vORS='\n\n\n\n\n\n' 1 file Or shorter version: awk 'ORS="\n\n\n\n\n\n"' file or avoid setting ORS for each input line: awk 'BEGIN{ORS="\n\n\n\n\n\n"};1' file


7

You seem to need column: paste file1.txt file2.txt | column -tc2 which creates this output: hi 1 wonderful 2 amazing 3 sorry 4 superman 5 superhumanwith 6 loss 7 You can of course also write your own script to do the formatting. Here is one way using awk: awk ' NR==FNR { a[FNR] = $0 ; if ...


6

Per the POSIX standard's page on sed: The script shall consist of editing commands of the following form: [address[,address]]function where function represents a single-character command verb from the list in Editing Commands in sed, followed by any applicable arguments. So the first non-blank character after the address is taken as a command ...


6

Tell awk to print between the two delimiters. Specifically: awk '/\*{4,}/,/<np>/' file That will also print the lines containing the delimiters, so you can remove them with: awk '/\*{4,}/,/<np>/' file | tail -n +2 | head -n -1 Alternatively, you can set a variable to true if a line matches the 1st delimiter and to false when it matches the ...


6

There are two basic approaches one can use when dealing with fields: i) use a tool that understands fields; ii) use a regular expression. Of the two, the former is usually both more robust and simpler. Many of the commonly available tools on *nix are either explicitly designed to deal with fields or have nifty tricks to facilitate it. 1. Use a tool that ...


6

Think this is what you want using GNU sed sed -n '/^PATTERN_START/,/^PATTERN_END/{ //!{H;/^Record/!{x;s/\n\([^\n]*\)$/ \1/;x}}; /^PATTERN_START/{h};/^PATTERN_END/{x;p;x;p};d };p' file Explanation sed -n #Non printing '/^PATTERN_START/,/^PATTERN_END/{ #If the line falls between these two patterns execute the next block ...


6

This was the best I could come up with: sed -n '/^PATTERN_START/, /^PATTERN_END/{ /^PATTERN_START/{x;s/^.*$//;x}; /^Record/{x;/^\n/{s/^\n//p;d};s/\n/ /gp}; /^PATTERN_END/{x;/^\n/{s/^\n//p;d};s/\n/ /gp;g;p}; /^Record/!H }; /^PATTERN_START/, /^PATTERN_END/!p' Explanation I assume you are ...


6

The e flag of the s command of the GNU implementation of sed is to evaluate the content of the pattern space after the substitution has been applied (successfully), and replace the pattern space with its output, not to evaluate the substitution. Here, for an input like: foo 1234 123 You'd need the subsitution to result in the pattern space containing: ...


6

Try this: find /tmp/ -type f -name "*.h" -o -name "*.cpp" \ -exec sed -i '1s/^/#include <stdint.h>\n/' {} + Also, as correctly pointed out to me, the ! -name "*.bak" is superfluous. The -name *foo pattern only matches files ending with foo. Therefore, the *.cpp and *.h already exclude *.bak.


5

It seems command substitution and braces are misused. NrLines=$(wc -l < t.txt) sed -i -e 1,"${NrLines}d" t.txt


5

sed -e 's/\(..\)\/\(..\)\/\(....\),\(.....\),\(.*\)/\3-\1-\2 \4:00,\5/' Edited to include the input from the comments below: sed -e 's#\(..\).\(..\).\(....\),\(.....\),#\3-\1-\2 \4:00,#'


5

If portability across unices is a concern, use ed: ed file <<END 1s/^/insertedtext/ w q END


5

In addition to @terdon's answer, with awk (and sed) you can use range pattern: awk '/sep1/,/sep2/{print}' file or sed -n '/sep1/,/sep2/p' file will print everything (including) sep1 and sep2. That is: ~$ awk '/sep1/,/sep2/{print}' file sep1 thingsIwantToRead1 thingsIwantToRead2 thingsIwantToRead3 sep2 In your case: ~$ awk '/\*\*\*/,/^$/{print}' ...


5

head; head { head -n[num] >/dev/null head -n[num] } <infile >outfile With the above you can specify the first number of lines to strip off of the head of the output w/ the first head command, and the number of lines to write to outfile with the second. It will also typically do this faster than sed - especially when input is large - ...


5

For the case above, you can do it like this: gsed 's/|/1)/; s/|/2)/; s/|/3)/; s/|/4)/; s/|/5)/' Example: $ echo '| | | | |' | sed 's/|/1)/; s/|/2)/; s/|/3)/; s/|/4)/; s/|/5)/' 1) 2) 3) 4) 5) This works if you can estimate in advance the maximum number of | on a line, and add s/|/N)/ accordingly. If you can't estimate the maximum number of | on a line ...


4

The typical solution with awk: awk ' NR==FNR { k[$1] = $2; next } { print $1, (k[$1] == $2) ? "Updated" : "Out-of-date" } ' local.txt server.txt


4

Perl to the rescue: perl -ne 'if (/\\$/) { $l .= $_ } else { print $l, $_ if $l =~ /XXX/; $l = ""; }' foo.txt $l works as an accumulator. -n processes the input line by line (cf. sed), if the line ends in a backslash, it's added to the accumulator, if not, the accumulator plus the line is printed provided it matches ...


4

With POSIX sed: $ sed -e ' :1 /\\$/{N s/\n// t1 } /\\/!d s/\\[[:blank:]]*//g ' file


4

POSIX one: $ { printf %s insertedtext; cat <./input_file; } >/tmp/output_file $ mv -- /tmp/output_file ./input_file


4

pr I'd probably go w/ pr: printf %s\\n hi wonderful amazing sorry \ superman superhumanwith loss >/tmp/file #^what is all of that, anyway?^ seq 7 | pr -tm /tmp/file - pr can -merge input files (here /tmp/file and - stdin) line-by-line like paste column-wise, but it can take many other parameters besides. By default it will print headers ...


4

sed '/./s/^/\\section{}/' Would prepend \section{} to every line that contain at least one valid character. sed '/^$/!s/^/\\section{}/' Would prepend \section{} to every non-empty line (that is lines that contain at least one byte). sed 's/./\\section{}&/' Would insert \section{} before the first valid character in every line (that has such a ...


4

With sed: $ sed -n -e 's/dog$/&/p' -e 's/cat$/&/p' file email1.com dog email3.com cat Or you can use awk: awk '$2~/^dog|cat$/' file


4

Another awk solution: awk '{print; getline < "file1"; print}' file2


4

And there's uniq --ignore-case --count | sort --numeric --reverse: uniq -ic /tmp/foo.txt | sort -nr 8 hot chocolate 6 Xicolatada And to switch around the order putting a comma in there add on: ... | sed -e 's/^ *\([0-9]*\) \(.*\)/\2, \1/'



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