New answers tagged

0

This solution user "123" created for me on another question was able to strip suffixes reliably without mangling words. I wanted to come back and answer this question so that anyone seeking a similar solution could get a good answer. awk 'FNR==NR{a[$0 "s"]++;next}!($0 in a)' file.txt file.txt awk 'FNR==NR{a[$0 "ed"]++;next}!($0 in a)' file.txt file.txt awk '...


0

Unix have these wonderful little tools called cut and paste. The cut tool will extract a set of columns from its input while the paste tool inserts columns. We will be using these. I'm not going to care about your pipeline too much (right now, but see the end of this answer), I'm just concerned with the problem of switching the columns around. Let's say I ...


1

Assuming that they are always the same amount of lines, you could do something like this: sed '/Connect\s*user@localhost on/,+7d' log.file This will remove the line containing Connect user@localhost on and the following 7 lines from the file "log.file" in your current directory. Edit: final solution (well, at least good enough for the OP to alter to his ...


1

The problem was that my editor was being stupid and I thought it was word wrapping. When I put sed -i.bak -c 's|net.ipv4.ip_forward.*|net.ipv4.ip_forward = 1|' /etc/sysctl.conf Into the editor the "/etc/sysctl.conf" would be touching the edge of the window so it displayed part of it on the next line. I thought it was just word wrapping but nope. Centos ...


-1

Using only sed (with -r flag for extended regex) echo "aaa string1 bbb aaa string2 bbb aaa string3 bbb" | sed -r 's/(aaa|bbb) ?//g' Returns string1 string2 string3 You also have this version using tr and grep (with -vE): echo "aaa string1 bbb aaa string2 bbb aaa string3 bbb" | tr ' ' '\n'| grep -vE '(aaa|bbb|^$)' Returns string1 string2 string3 ...


1

If your system's grep supports PCRE, you could maybe do $ echo 'aaa string1 bbb aaa string2 bbb aaa string3 bbb' | grep -oP '(?<=(aaa|bbb) )\w*?(?= (aaa|bbb))' string1 string2 string3 or if you need to handle more general amounts of surrounding whitespace $ echo 'aaa string1 bbb aaa string2 bbb aaa string3 bbb' | grep -oP '(aaa|bbb)\s+\K\w*?(?=\s+...


0

If you are fine with something structured like: string1 string2 string3 I would just simply replace you delimiters with a newline. Something such as this should get you close: sed "s/\(aaa\)\|\(bbb\)/\n/g" test.txt Edit As pointed out by @clk below, my first answer may give double newlines. Changing to something such as: sed "s/\(\s\)\?aaa\(\s\)\?/...


2

Here's one way to do it with sed: sed '/Os version rhel5\.6/{ a\ apache 4.2 $!{ n /^apache 4\.2$/d } }' infile This appends apache 4.2 unconditionally to all lines matching Os version rhel5.6 then (if not on the last line) it pulls in the next line via n (printing the pattern space) and if the new pattern space content matches apache 4.2 it deletes it. ...


2

This perl expression will do the trick, perl -i -ne 'next if /apache 4.2/;s+Os version rhel5.6+Os version rhel5.6\napache 4.2+; print' ssss Explanation next if /apache 4.2/ skips any lines matching apache 4.2. s+Os version rhel5.6+Os version rhel5.6\napache 4.2+; print search Os version rhel5.6 and replaces line with same with appending apache 4.2 at ...


3

With gnu sed/shuf: sed '1b;s/^*/\x0*/' infile | shuf -zn 5 | tr -d '\000' This turns input into nul separated records i.e. on each line that starts with a * (except for the first one) it adds a nul char before the * then uses shuf with --zero-terminated switch to extract five random records and tr to delete those nul chars.


2

cat listfile | tr '\n' , | sed 's/,\*/\n*/g;s/,$//' | shuf | head -n 5 | tr , '\n'


0

Your main problem (the one that causes a syntax error) lies on the sed line: sed -n '$j,4800p' IM_DlCtrlRef.txt >> IM_DlCtrlRef_bak You want to use the shell variable $j in you sed script as the start line number (the sed script extracts lines $j to 4800 out of a file), but you prevent the shell from expanding the variable by using single-quotes. ...


0

With sed: sed -e 's/\<\([0-9.]\+\)-/-\1/g' or sed -E -e 's/\<([0-9+]+)-/-\1/g' With GNU awk: awk '{$0=gensub(/\<([0-9.]+)-/,"-\\1","g"); print }' NOTE: requires GNU awk for gensub() function. Neither sub() nor gsub() support capture groups. With perl: perl -p -e 's/\b([\d.]+)-/-$1/g'


0

If you want to pass the output to less you don't need a sed command at all, just set how less should highlight searched pattern (e.g. in .bashrc): export LESS_TERMCAP_so=$'\e[93m' # begin standout mode export LESS_TERMCAP_se=$'\e[m' # end standout mode and then run less with -p option: less -p '.*DATA.*' file


3

Perhaps you meant to use less on the output of sed, rather than the reverse: sed -e 's/\(.*DATA.*\)/\o033[93m\1\o033[39m/' file | less -R Further reading: less - opposite of more


0

Instead rely on the SQL to fix the problem: create table #Tempt (Value VarChar(100)) Insert Into #Tempt Values(NULL) Insert Into #Tempt Values('500.1-') Insert Into #Tempt Values('-500.1') Insert Into #Tempt Values('20.5') select *, case when Value like '%-' then '-' + replace(value,'-',') else value end From #Tempt Then use the bulk insert to pull ...


1

you can give multiple instruction in one shot of sed, for example: sed 's/\t/ /g;/^ *$/d;s/^#/NODIESE/' testfile this single line replace tab with space delete line that start with empty stuff (or empty line) replace Dash at start with the word NODIESE so your test file is processed only once and you launch sed only one time.


2

With sed: echo 00000012- | sed -e 's/\([0-9]+\)\(-\)\?/\2\1/' gives -00000012. That is, matching two groups, the first with digits and the second with a sign, but using zero-or-more matches of that, and then replacing the two interchanged.


0

If I understand your index generation correctly, then awk '{print 5*(NR-1)+1" "$0}' yourfile > oufile should do it. If you want prettier output, you can use printf instead e.g. $ awk '{printf "%-3d %s\n", 5*(NR-1)+1, $0}' yourfile 1 4.184 4.2648 6 4.2281 4.0819 11 4.2204 4.1676 16 4.0482 4.1683 21 4.0156 4.2895 26 4.4504 5.2369 31 4....


0

Create an index-column on the original data file, using pr -t -n. Create the index data to be inserted as the new column, with each row of index data indexed by the row number. I used a little bash function to do this below. Join the index column with the data using join. Here's a bash script to demonstrate: #!/usr/bin/env bash # insert-counts.sh cols='/...


3

There is more than one problem, seen by making a script of your examples: #!/bin/sh MSG="this is MAJOR stuff" echo "$MSG" | sed -e 's/\(.*MAJOR.*\)/\o033[93m\1\o033[39m/' echo "$MSG" | sed -e 's/\(.*MAJOR.*\)/\033[40;5;95;38;5;202\033[0m/' echo "$MSG" | sed -e 's/\(.*MAJOR.*\)/\o033[40;5;95;38;5;202\o033[0m/' echo "$MSG" | sed -e 's/\(.*MAJOR.*\)/\033[38;5;...


2

You are probably looking for this: sed -e 's/\(.*MAJOR.*\)/\o033[48;5;95;38;5;202m\1\o033[0m/' although it can be simplified with sed -e 's/.*MAJOR.*/\o033[48;5;95;38;5;202m&\o033[m/' Notice change of 40 to 48 what means we want to change background color. If you want background color as black: sed -e 's/.*MAJOR.*/\o033[40;38;5;202m&\o033[m/' ...


2

I think this does what you want; it accepts an awk variable named "factor" that is can easily be set to whatever you want: awk -v factor=8.06573 '{printf "%2.9f %2.9f\n", $1 * factor, $2 * factor}' With the given input, it outputs: 34.193855762 35.948152037 34.220472671 33.078365303 34.585043667 33.260650801 33.961562738 36.169959612 34.176917729 33....


0

with GNU sed sed -r 's/^([^.]+)\.[0-9]+ /\1 /' filename ^([^.]+) capture starting string upto first dot character \.[0-9]+ match dot character followed by more than 1 digit characters and if number of characters is consistent as given in the example, sed -r 's/^(.{8}).{7} /\1 /' filename


2

I would use awk awk --posix '{ gsub(/\.[[:digit:]]{6}/, "", $1); print }' filename Will target the first field (space delimited) and search for a . followed by 6 numbers and empty it out.


1

With awk probably the simplest way is to do a regular expression substitution on the first whitespace-separated field, replacing everything from the period to the end of the field: awk '{sub(/\..*/,"",$1)}1' somefile


0

I tend to use Perl. sed or awk is fine in this case, but sometimes the flexibility is Perl is useful. perl -pi -e 's/^(mynetworks.*)/$1 0.0.0.0\/0/' /path/to/file or if this is in a pipe chain cat file | perl -pe 's/^(mynetworks.*)/$1 0.0.0.0\/0/'


1

To add the specified text to a line in the file - if that line is the only one that starts with mynetworks, you can do this: sed --in-place '/^mynetworks/s_.*_& 0.0.0.0/0_' /path/to/file


3

Using sed sed -i 's+^mynetworks.*+& 0.0.0.0/0+' log.txt Using awk awk '/^mynetworks/ {$0=$0" 0.0.0.0/0"} 1' log.txt or awk '{if ($1 ~ /^mynetworks/) print $0, "0.0.0.0/0"; else print $0}' log.txt Using bash while read -r line ; do [[ $line == mynetworks* ]] && line+=" 0.0.0.0/0" echo "$line" done < log.txt


1

With GNU sed $ sed ':a;N;$!{/\n$/!ba}; s/[[:blank:]]*\n[[:blank:]]*/ /g' textfile Start of p1 continued p1 end of p1. Start of p2 end of p2. Start of p3 continued p3 even more p3 end of p3. How it works :a This defines the label a N This reads in the next line and appends it, along with a newline character, to the current line. $!{/\n$/!ba} If (a) ...


2

A grep solution: $ echo "2.5 test. test -50.8" | tr ' ' '\n' | grep -E '^[+-]?[0-9]*\.?([0-9]+)$' 2.5 -50.8 The tr just converts the line into multiple lines by replacing the spaces with newlines. The grep command looks for strings that starts with an optional + or -, possibly followed by some digits and an optional decimal point. Then we require some ...


4

grep works well for this: $ echo "2.5 test. test -50.8" | grep -Eo '[+-]?[0-9]+([.][0-9]+)?' 2.5 -50.8 How it works -E Use extended regex. -o Return only the matches, not the context [+-]?[0-9]+([.][0-9]+)?+ Match numbers which are identified as: [+-]? An optional leading sign [0-9]+ One or more numbers ([.][0-9]+)? An optional period followed ...


0

try sed -e 's/^.*"\([^" ]*\)"".*/\1/' log | sort | uniq egrep -o '[^"]+@[^"]+' log | sort | uniq where -o print only matched pattern [^X]+ any number (> 0) of char different from X please note sed solution relays on a typo/feature in your file (double double quote) grep solution relays on foo@some.where pattern awk (or perl for that matter) is ...


0

I would try something like: awk -v pattern="Testing:" '$0 ~ pattern { sub(pattern, " *"); print }' Frankly, I don't see why you are messing around with the newlines.


0

use c (change operator) in sed: sed -e '/ServerName/c ServerName www.mydomain.com' FILENAME


0

Using sed substitue command: sed -i 's/ServerName/ServerName www.mydomain.com/' file.txt


1

sed --in-place 's/ServerName/& www.example.com/' /path/to/apache-vhost.conf


1

Using sed: sed 's/^\(ServerName\)$/\1 www.mydomain.com/' file.txt The captured group, \(ServerName\) is used in the replacement pattern as \1. Editing the file in place, with backup, assuming the GNU, ssed, busybox or some BSD implementations of sed: sed -i.bak 's/^\(ServerName\)$/\1 www.mydomain.com/' file.txt The original file will be kept as file....


4

The sed script consists of three substitute commands. Substitute commands are of the form s/old/new/ which looks for something in the text that matches regular expression old and replaces it with new. If a g is put after the command, then this substitution is done repetitively ("globally"). The first one removes periods. The second one makes the text ...


1

With GNU sed: sed -i '1i\\t'$sentence /tmp/2 or sed -i "1i\\\t$sentence" /tmp/2 The text to insert starts after the first \. Single-quote the sed script to stop your shell from doing thing with the \t (or insert yet another \ as in the second example above). With single quotes, concatenate $sentence with the sed script so that it gets expanded by the ...


2

While sed may be able to interpret \t in a regular expression, none of your script uses it that way. Rather, it is using \t as a literal string. You could do what you want like this: printf "Masi \nwas \nhere" > /tmp/2 sed -i "1i XwordXinclude word'" /tmp/2 sed -i '1,1s/X/\t/g' /tmp/2


4

The expressions in the apostrophes are not evaluated (nor subshell nor variables). You need to use normal quotes: $ pos=2; $ printf "Masi \nwas \nhere" > /tmp/1 $ sed -i "`echo $pos`i huhu" /tmp/1 $ cat /tmp/1 Masi huhu was here which is equivalent of $ sed -i "${pos}i huhu" /tmp/1 (without the subshell)


2

The cut utility "cuts" one or several columns out of its input. Like many other Unix utilities, cut is a stream-based line-by-line reader that applies its processing to each line of input and then contiues with the next line. You can not use \n as the delimiter for cut since that is what it expects separate the lines. A roundabout way of creating two ...


0

I found this very useful for collecting log files from S3 Cloudfront and loading them into Google Drive. I used awscli on Mac-Os after installing this with homebrew awscli. I ran the command : aws s3 sync s3://bucketname/domain/ . The bucket was filled with cloudfront logs (I turn this on at the cloudfront edit) and all the files where pulled to my ...


3

#! /usr/bin/awk -f /"dvorak"/ {dvorak++}; /{/ && dvorak {b++} ; /}/ && dvorak {b--} ; dvorak && b == 0 && NR > 1 { print NR; exit } $ ./find-dvorak.awk /usr/share/X11/xkb/symbols/us 248 This uses a counter (b) which gets incremented every time it sees an open-curly-bracket { and decremented whenever it sees a ...


4

This sed script prints the line number of the line matching /^};/ in the range of lines from /xkb_symbols "dvorak" {/ to the next /^};/ (which will be the same }; as the one we get the line number for): /xkb_symbols "dvorak" {/,/^};/{ /^};/= } If you need both start and end line numbers: /xkb_symbols "dvorak" {/,/^};/{ /xkb_symbols "...


3

Since you're not actually changing the "cast" line: sed '/cast \$recv \$UE_CAPABILITY_ENQUIRY/{a\ set trans_id 1 n;d}' file As Kusalananda comments, this command: when one of the wanted "cast" lines is found: append the new line take the next line from the file (the unwanted "set" line) and delete it In hindsight, this does not ...


0

A sed solution: /:\./! { s/^.*://; H } /:\./ { G; s/\n/ /g; s/\. //; s/$/./p; s/.*//; x }


0

$ cat tst.awk BEGIN { FS=":" } { if ($2==".") { ORS = "\n" OFS = "" cnt = 0 } else { ORS = "" OFS = " " cnt++ } print (cnt==1 ? $0 : OFS $2) } $ awk -f tst.awk file student:xxxxx yyyyy zzzzz kkkkk. teacher:aaaaa bbbbb lllll. student:sssss mmmmm. If it's not obvious how that works you can ...


0

None of the answers above mention how to save the changes to the original file, which is I think what the OP was asking for. It's certainly what I needed when I cam to the page. So assuming your file is called output.txt sed -i '1 a This is the second line' output.txt My particular use case is to automatically add an xsl stylesheet declaration to a ...



Top 50 recent answers are included