Tag Info

Hot answers tagged

19

To replace a single 0 on a line: sed 's/^0$/x/' ^ matches the beginning of the line $ matches the end of a line So the above command matches the beginning of a line, followed by 0, followed by the end of the line.


13

Use this: nl file | shuf -n2 | sort -n | cut -f2- nl to number the lines, shuf to shuffle and limit the output to 2 lines (-n), sort to rebuild the original order, and cut to remove the numeration of nl. It will print 2 lines of your file in the original order of the file. Use shuf -n X, where X can be any number.


13

cut -d / -f 4 file1.txt | paste -sd '|' | xargs -I{} grep -v -E {} file2.csv Explanation: cut -d / -f 4 file1.txt will select the hashes from the first file paste -sd '|' will join all the hashes into a regular expression ex. H1|H2|H3 xargs -I{} grep -v -E {} file2.csv will invoke grep with the previous pattern as an argument, xargs will replace {} with ...


13

With GNU sed: sed -n '132 {/^#termcapinfo[[:space:]]*xterm Z0=/q}; $q1' How it works 132 {/^#termcapinfo[[:space:]]*xterm Z0=/q} On line 132, check for the regex ^#termcapinfo[[:space:]]*xterm Z0=. If found quit, q, with the default exit code of 0. The rest of the file is skipped. $q1 If we reach the last line, $, then quit with exit code 1: q1.


12

This works, as indicated by jasonwryan: awk 'BEGIN{print "START"}; {print}; END{print "END"}'


11

Possible awk solution: awk 'NR == FNR { x[$4] = 1; next; } { if (!($1 in x)) print $0; }' FS="/" file1.txt FS="," file2.txt First we read file1.txt using FS (field separator) "/" and create array x with keys values from field $4 which is the hash you want. Next we read second file file2.txt setting FS to be , and check if value of field $1 does not exist ...


10

You should not parse xml with sed, use an xml parser like xmlstarlet instead. For your task it would be: xmlstarlet ed -O --inplace --insert "/book" --type attr -n Book_Width -v A xml_file The file content is then: <book name="Sed tutorial" price="250" Book_Width="A"/> The ed means edit mode to edit the xml tree -O omits the xml tag We want to ...


8

Your command does not work because of the dashes before the <Directory ...> (and </Directory>) statements. This should work: sed '/<Directory "\/var\/www\/html">/,/<\/Directory>/d' /etc/httpd/conf/httpd.conf Also, to make this more readable, you may want to use another character as delimiter than /, for example, #, like so; sed ...


8

This can be done in sed with sed -e $'1i\\\nSTART' -e $'$a\\\nEND' 1i means insert before line 1; $a means append after the last line.  The $'…' syntax is bash-specific.  In other shells, you should be able to do this with: sed -e '1i\Enter START' -e '$a\Enter END'Enter


7

If you're already using sed, you can use 1 to match the first line and $ to match the last line (see Scott's answer). If you're already using awk, you can use a BEGIN block to run code before the first line and an END block to run code after the last line (see Michael Durrant's answer). If all you need to do is add a header and a footer, just use echo and ...


6

The right tool for this job is nl: nl -w2 -s'> ' file You may want to tune width option according to the total number of lines in the file (if you want numbers to be aligned nicely).


6

sed 's:\\1\\:\ :g' file you will have to escape the backslashes in your match pattern. In the replacement, that's a backslash followed by a literal newline character. Some sed implementations, like GNU sed also support \n there as an non-standard alternative. Output "evSchema" "UAT" "evSN" "uadb" "evDirsep" "/" "evRootPath" "/work_area/APP_UAT/" ...


6

With a slight modification to your XML, wrap all your XML in a parent <DATA> tag1, or another one of your choosing, file called data.xml: <DATA> <HARDWARE> <NAME>WIN1</NAME> <OS>Windows 7</OS> <IP>1.2.3.4</IP> <DOMAIN>contoso.com</DOMAIN> </HARDWARE> <HARDWARE> ...


6

If I understand correctly, you want to change _x to X as long as it occurs inside '...' strings. Then, with GNU sed, you could do: sed -E ":1;s/^(([^']|'[^']*')*'[^']*)_([^'])/\1\u\3/;t1" That is replace a _X following '... itself following a sequence of either non-quotes or matched quotes. Which on an input like: foo_bar 'ab_cd_ef gh_ij' zz_zz ...


5

The selection of a random line from a file without sorting (or even knowing how many lines there are!) is covered in "The Art of Computer Programming", Volume 2, Section 3.4.2 by Donald E. Knuth. This is trivial to implement, e.g.: (echo foo; echo bar; echo zot) \ | perl -nle 'rand $. < 1 && ( $line = $_ ); END { print $line }' Or try shuf, ...


5

For GNU sed sed -z 's%.*/\([^/]*\)/index.html\n%\1\\|%g;s%^%/%;s%\\|$%/d%' file1.csv | sed -f - file2.csv where first sed produce list of hashes in sed-command-format like /12ab09f46\|a77b3ff22\|..../d and transfer it to next sed-script which reads above command from input therefore -f - option. Same with grep grep -oP '[^/]*(?=/index.html$)' file1.csv | ...


5

With GNU sed: sed '/example\.com/d;/test\.com/d' -i file.txt will remove the lines with example.com and test.com. From man sed: d Delete pattern space. Start next cycle.


5

With POSIX toolchest: tail -n +132 </etc/screenrc | head -n 1 | grep -q pattern


4

This has been answered before elsewhere on Stack Overflow. http://stackoverflow.com/questions/15361632/delete-a-column-with-awk-or-sed http://stackoverflow.com/questions/7551219/deleting-columns-from-a-file-with-awk-or-from-command-line-on-linux etc.. I believe awk is the best for that. awk '{print $1,$2,$3,$4,$5,$7}' file It is possible to use cut as ...


4

You could do it with a while loop and $RANDOM like: while read line; do if ((RANDOM%2)); then echo $line; fi; done < _path_ which will print about half the lines, which you could control with the condition in the if


4

Line 1: Your hashbang line is not correct, use: #!/bin/bash Line 3: Take care with the test utility (it needs a space before the closing ]): if [ -f KeyFile ] Line 5: In the sed command, use -i to activate in-place editing of sed, else the edits are only printed to the stdout: sed -i 's/[0-9][0-9]NM/Okay/g' KeyFile


4

The reason is probably that / (containing /etc) is a read only filesystem, but has a symlink for /etc/shadow, /etc/passwd, and other dynamic files that lands on a read-write filesystem. This will allow you to edit the shadow and passwd files directly. The sed -i fails because its implementation doesn't actually update in place. Rather, it creates a ...


4

Another awk: $ awk 'BEGIN{getline l <"file1"};{print $0, l}' file2 1 2 3 12 4 5 6 12 7 8 9 12 BEGIN block was executed first before reading input file. The first line in file1 was retrieve using getline() function, stored in variable l With each line of file2, we print it content $0 along with l, separated by OFS, which is a space by default.


4

Possible awk solution could be: awk 'BEGIN { RS = ""; } { $1 = $1; } 1' matrices.txt > modified_matrices.txt


4

On a GNU system, you can use this: sed -i '/^#[[:blank:]]Person/{n;s/#root:[[:blank:]]\+marc/root:\tsomeone@something.tld/;}' file It searches for a line beginning with # Person. Then switches to the next line and replaces #root:<blanks>marc with root:<tab> .... The -i flag edits the file inplace. -i, \+ and \t are GNU extensions. The ...


4

try awk '$1 == "," && NF==1 { next ; } { print ; } ' file > other_file or grep -v '^[ ]*,[ ]*$' file > other_file or sed -i -e '/^[ \t]*,[ \t]*$/d' original_file


3

If you want the same format that you have specified awk '{print NR "> " $s}' inputfile > outputfile otherwise the cat command can print line numbers for you. cat -n inputfile > outputfile


3

in sed "a" appends a pattern IN A NEW LINE. what you want to do is replace (substitute). Let's use a colon as separator for clarity: sed 's:\(<book.*\)\(/>\):\1 Book_Width="A"\2:' anything in \( .. \) is a pattern memorized by the order of appearance and recalled by \indexnumber , e.g. \1 will reproduce the first pattern saved. So we are ...


3

awk 'FNR == NR && $0 !~ /^[[:blank:]]*$/ { Dict[$0] = 1 } FNR != NR { i = 1 while( i <= NF && Dict[ $i] == 1) i++ if( i > NF) print } ' File1.txt File2.txt generic, non dependant to file2 number of field/word per line work with sorted and unsorted content of both file use memory to load first ...


3

With the -F option, grep looks for exact matches (regex features turned off): $ grep -F "defabc" xyz.txt defabc grep sets an appropriate return code so that we can test for true or false: $ if grep -qF "defabc" xyz.txt; then echo True; else echo False; fi True $ if grep -qF "Defabc" xyz.txt; then echo True; else echo False; fi False Because we only ...



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