Sundeep
  • Member for 6 years, 9 months
  • Last seen more than a week ago
How to use sed to replace numbers with parenthese?
2 votes

Assuming input has : separated text and 2nd column needs to be changed: $ awk 'BEGIN{ FS=OFS=":" } {$2 = "("$2", 0)"} 1' ip.txt hello:(123, 0): world hello:(783, 0): world hello:(479, 0): world Or, ...

View answer
Replace all occurrence of character except first one
2 votes

$ cat ip.txt test1,test2,test3 test4,test5 test6,test7,test8,test9,test10 test11,test12,test13,test14 test15 $ perl -F, -ane 'print "$F[0]"; print ",".join(";",@F[1..$#F]) if($#F > 0)' ip.txt ...

View answer
Remove newline character just every N lines
2 votes

Generic solution, replace 5 with number of lines required $ # eof to ensure last line has newline ending $ seq 16 | perl -pe 's/\n/ / if ++$i%5 && !eof' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

View answer
how to CREATE a column with zero values in shell?
6 votes

perl -e 'print "0\n" x 1000' > file.txt As @Stéphane Chazelas notes, this is fast for large numbers but can run into memory issues(use yes|head approach in that case) performance comparison, ...

View answer
How to append some string combined with the pattern at the end of line with the pattern
1 votes

Solution using Perl, does inplace editing perl -i -pe 's/(\s\S+?)(\.?)\s*$/$1$2$1.com\n/' /etc/hosts \s match a white-space character \S+? non-greedy match 1 or more non-white-space characters \.? ...

View answer
How to align list to specific character?
6 votes

hacky solution, assumes a lot about input text $ # four commas to reduce chance of it affecting actual email address $ sed 's/@/,,,,@/' ip.txt | column -t -s,,,, 123 @example.com 456789 @example....

View answer
How to grep all value starting with "sg" in my case
Accepted answer
3 votes

Try this: $ grep -o 'sg-[^ ]*' ip.txt sg-77aaaa10 sg-7842031d sg-796d1b1e sg-79a4861d sg-79a4861d [^ ]* means other than space character. You can also use [0-9a-f] if you know they consist of only ...

View answer
Replace range of lines with range of lines (sed or others)
5 votes

Another way to do with sed is using r command, handy if -i inplace option has to be used as well $ sed -n '3,5p; 5q;' f2 | sed -e '2r /dev/stdin' -e '2,4d' f1 line one line C line D line E line five ...

View answer
trim tailing - and add it leading for Numbers in shell
0 votes

With perl, but not suitable for all sorts of csv formats, works for given sample $ perl -ne '@cols=/"[^"]+"|[^,]+/g; map {s/"(.*)-"$/"-$1"/ or s/(.*)-$/-$1/} @cols; print join ...

View answer
Select lines of file 1 where field of file 2 > x
2 votes

With awk and getline (see all about getline for caveats) $ # can also use: awk '{getline num < "file2"} num>0.5' file1 $ awk -v cmp_f='file2' '{getline num < cmp_f} num>0.5' file1 ...

View answer
Count number of columns in CSV files and rename if less than 11 columns
0 votes

perl has rename built-in function (with caveats, so check the documentation) perl -F, -ane '$f=$ARGV; $c=$#F+1; close ARGV; rename $f,"$f.bad" if $c<11' *.csv -F, use , as field separator, see ...

View answer
Ignore part of the string with sed
3 votes

If you are okay with using perl $ s='abcTestString : "abc {ccb} bbc",' $ echo "$s" | perl -Mopen=locale -Mutf8 -F: -lane ' $F[-1]=~s/\{[^{}]+\}(*SKIP)(*F)|[a-z]+/$&=~tr|abc|абц|r/...

View answer
With the help of line number, replace a whole line with another line which cotains file path
0 votes

Another way is to use c command which clears the line(s) specified by address $ seq 5 | sed '2c\ foobar' 1 foobar 3 4 5 $ # with GNU-sed, assuming \ is not first character $ seq 5 | sed '2cfoobar' ...

View answer
Delete lines x after a match
2 votes

For generic cases, see this excellent answer from Ed Morton: Printing with sed or awk a line following a matching pattern . d) Print every record except the Nth record after some regexp: awk 'c&...

View answer
Change a list of strings to lowercase
Accepted answer
3 votes

With GNU sed, assumes that you do not have any meta character in list of strings, + is not a meta character with default BRE $ # create substitute command for each line $ sed 's/.*/s|"&"|\\L\&...

View answer
Sorting lines in a file by a numeric value burried inside
Accepted answer
8 votes

Use sort -V if that option is available -V, --version-sort natural sort of (version) numbers within text $ <cmd> | sort -k5,5V 2017-10-25_09:30:22/sdq.log:Data LOST: 2.00 KB (4 sectors) 2017-...

View answer
using awk to print lines from one match through a second instance of a separate match
7 votes

One way is to use flags and counter to keep track $ awk '/b/{f=1; c=0} f; /d/ && ++c==2{f=0}' file.txt b c d e f d /b/{f=1; c=0} set flag for starting match and initialize counter f; print ...

View answer
Process /etc/passwd file to list all users whose home folder is in /home
3 votes

You can also use index for fixed string matching like grep -F and use the return value to ensure it matches start of string $ awk -F: 'index($6,"/home/")==1{print $1}' ip.txt tom george bla boo or ...

View answer
How to make "incremental" replace in files with bash
1 votes

$ cat f1 My mistress' eyes are nothing like foobar; foobar is far more red than her lips' red; $ cat f2 Shall I compare thee to a summer's day? Thou art more lovely and more foobar: With perl $ perl ...

View answer
sed + remove character on the first match or on the second match
Accepted answer
0 votes

Use awk instead $ cat ip.txt # Allow from All # Require all granted # # Apache 2.2 # Require all granted # Order Allow,Deny # Require all granted # Order ...

View answer
Remove SSIDs without name in iwlist wlan scan
Accepted answer
3 votes

$ iwlist wlan0 scan | grep 'ESSID:"..*"' ESSID:"OpenWrt" ESSID:"dlink" ESSID:"tplink" ESSID:"linksys" ESSID:"tenda" ESSID:"pi" ...

View answer
Cut the SUBSTRINGS to a specific length in a CSV file
0 votes

With perl Assuming ; is only in third field $ perl -pe 's/"\K[^;"]*;[^"]*(?=")/$&=~s|([^;]{2})[^;]+|$1|gr/e' ip.txt "pav",12345,"AB;xy;15",, "xyz",,"C4;x2;rt",, "\K to match " before string of ...

View answer
How to filter and log one instance of a line
1 votes

Some comments and suggestions Avoid Useless Use Of Cat. Many commands can directly accept filename as input and if not, use re-direction. For ex: tr 'a-z' 'A-Z' < ip.txt or < ip.txt tr 'a-z' 'A-...

View answer
Getting only specific data based on name in text file
3 votes

Would suggest to use awk for this Default input field separator is one or more consecutive white-spaces, so no need to worry about space/Tab separations Easier to compare fixed strings (i.e less ...

View answer
Create lists of words according to binary numbers
6 votes

Another one with perl $ perl -lane 'if($. == 1){ @h=@F } else{@i = grep {$F[$_]==1} (0..$#F); print join ",",@h[@i]} ' ip.txt E,I D D A A,C,G A,D,H A,E,F,G -a option to ...

View answer
Append to file1 on differences of specific column between two files
Accepted answer
1 votes

Use awk when you need field based comparison $ awk -F, 'NR==FNR{ip[$2]=$0; next} $2 in ip{delete ip[$2]} END{for(k in ip) print ip[k]}' events.txt source.txt 01.02.70 08h00,4.4.4.4,443 -F, set , as ...

View answer
Is there an easy way to count characters in words in file, from terminal?
10 votes

Another way to do it all with awk alone $ awk '{words[length()]++} END{for(k in words)print k " character words - " words[k]}' ip.txt 2 character words - 3 5 character words - 1 7 character words - ...

View answer
How to use grep to search word with specific number of occurence
1 votes

$ cat ip.txt xe xa xaaaalt xaaa xaa $ # match 'a' 1 to 3 times $ grep -i '^xa\{1,3\}' ip.txt xa xaaaalt xaaa xaa $ # with ERE, no need to escape {} $ grep -iE '^xa{1,3}' ip.txt xa xaaaalt xaaa xaa ...

View answer
Replace line M in file1 with line N in file2
2 votes

Try this: $ cat f1 foo bar xyz baz temp good $ cat f2 1 2 3 4 5 6 $ awk -v m=3 -v n=4 'NR==FNR{if(FNR==n) s=$0; next} FNR==m{$0=s} 1' f2 f1 foo bar 4 baz temp good NR==FNR will be true only when ...

View answer
Printing the difference between adjacent values in a column to a new column
Accepted answer
5 votes

To modify the given code in question $ awk 'NR>1{$3=$2-p} {p=$2} 1' file A 1 A 2 1 A 4 2 A 6 2 Fields are indexed from 1, so use $2 for second column $0 contains entire input record After ...

View answer
1
3 4
5
6 7
10