Hot answers tagged

5

If the input is processed line by line, then processing needs to go like this: if the current line is foo.bar, store it, forgetting any previous foo.bar line that wasn't enabled for output; if the current line is relevant=yes, this enables the latest foo.bar for output. This kind of reasoning is a job for awk. (It can also be done in sed if you like ...


5

That result you're getting is because . matches any single character From manpage >REGULAR EXPRESSIONS:    The fundamental building blocks are the regular expressions that match a single character. Most characters, including all letters and digits, are regular expressions that match themselves. Any meta-character with ...


4

Try awk awk ' NR==FNR{ A[NR]=$1 limit=NR next } /^avocado/{ i=i%limit+1 $1=A[i] } { print } ' newservers.lst servers.txt Sed is possible too: sed '/^\s*\S\+\s*$/ { #match 1st file only x #exchange line with holdspace H ...


3

You could use join for this join -1 1 -2 3 -t ',' -a 2 -o 2.{1..9} 1.{2..3} <(sort file1.csv) <(sort file2.csv) -1 and -2 specifies which field from the files to compare -t specifies the seperator to use for the fields -a 2 says to print lines in <file2> that do not match -o configures the output based on <file>.<field>


3

My first thought was the same as Jeff Schaller's: cut -c 17-64. However that includes the closing quote, so you need cut -c 17-63 If you want the first quoted word and not every word is the same length, you can use awk: awk -F\" '{print $2}' file.txt


3

Here's one way with sed: sed '/foo\.bar/h;/relevant=yes/!d;x;/foo\.bar/!d' infile Lines matching foobar are copied to hold space. All lines, except those matching relevant=yes are deleted. Exchange hold space with pattern space (this only happens when lines match relevant=yes) and delete if it doesn't match foobar.


3

There is more than one problem with the script, plus the problem statement needs some clarification: the gsub call has the regular expression in the wrong parameter updating $1 has no effect on $0 (the value used in the print statement) OP did not clarify if the intent was to leave the last occurrence on a line untouched, or only the last line containing ...


2

My contribution is a POSIX-compliant ed alternative to Thomas Dickey's AWK; it makes the same assumptions: printf '%s\n' '1;?2016/01/30 14:52:51: ?' '1,.-g//s///' w q | ed filename ed's ability to traverse the file backwards makes short work of this task.


2

Just use a different delimiter: sed -i 's|foo|bar|g' filename So, in your case: sed -i 's|/home/saeid/public_html|/home/saeid/www/domain.com/html|g' file This is mentioned in info sed: The syntax of the s (as in substitute) command is ‘s/regexp/replacement/flags’. The / characters may be uniformly replaced by any other single character within any ...


2

You can get string length of the variable by using ${#variable}. And you should use -gt instead of > in the [ ] expression. #!/bin/bash pass=$(cat "$1") if [ ${#pass} -gt 32 ]; then echo "Error: Password length invalid" else echo "okay" fi


2

Here's one way with sed: sed -n 'H # append every line to hold buffer /CREATE\|REPLACE/h # if CREATE or REPLACE, overwrite hold buffer /;/{ # if line matches ; then x # exchange hold space w. pattern space /CREATE/w file1 # if pattern space matches CREATE, ...


2

$ gawk '{print > "file" ++a[$1] ".txt"}' input # And on OSX awk, and also gawk: $ awk '{print > ("file" ++a[$1] ".txt")}' input $ head file*txt ==> file1.txt <== 1 Cat 2 Boy 3 Tree ==> file2.txt <== 1 Dog 2 Girl 3 Leaf ==> file3.txt <== 1 Frog 3 Branch ==> file4.txt <== 3 Trunk edit: An ...


2

The best tool for the job is probably join but since you mentioned awk, here's another approach: $ awk -F',' -vOFS="," 'FNR==NR{a[$1]=$2;b[$1]=$3; next}{print $0,a[$3],b[$3]}' file1 file2 Converting,:Developer::|[E],android,Exact,,,,8,31,1,2 Converting,:Developer::|[E],osx,Exact,,,,8,31,2,5 Converting,:Developer::|[E],windows,Exact,,,,8,31,, It uses two ...


2

The simple command before the pipe in: OPTIONS=$VERSIONS | awk -F'[,]' '{print $1}' sets OPTIONS to $VERSIONS. Since there is no actual utility name there, the command is simply an assignment, and thus it produces no output. So nothing is piped to awk, and consequently awk does nothing. (And even if it did something, it wouldn't do it to $OPTIONS. Even ...


2

Since you're trying to match the first 30 lines of your files you could save the text in a file e.g. ref_file then use diff to compare the reference file with the first 30 lines in each file: find . -name "*.[ch]" -exec ./myscript {} \; -print where ./myscript is #!/bin/sh head -n 30 "$1" | diff - /path/to/ref_file >/dev/null so -print in the first ...


2

You could find each file to be processed using find and feed each filename to a script made on purpose to look for a match and print the filename in case of a match; I'd suggest to use a script rather than a one-liner for the added easiness of handling the multi-line string compared to the prompt. That is: find . -name "*.[ch]" -exec /path/to/script {} \; ...


1

In vim, :goto 6 or, 6go will move the cursor to the graphem that contains the 6th byte in the current buffer. If the offset points to a newline character, it will position the cursor at the end of the corresponding line (on you sample, 40go and 41go will bring the cursor to the same position).


1

Another way using od -- od -An -c -j 5 -N1 file i


1

Cut is what you are looking for. cut -b6 file1.txt This shows you the 6th byte...per line of the file. I think you only want the 6th byte so you can use something like head head -n1 file1.txt | cut -b6 Voila.


1

Try this: sort -n --k3 <file> For example: $ sort -n -k3 test HELIX cp9(plasmid 9586 bp DNA helix 29-AUG-2011 HELIX lp25(plasmid 24437 bp RNA linear 29-AUG-2011 HELIX lp28-1(plasmid 25455 bp DNA linear 29-AUG-2011 HELIX chromosome 911724 bp ...


1

awk -F'[,]' '/^[ ]/{print $1}' Would only act on lines that begin with a space, thus skipping those without such. Another option might be to look for lines containing a comma, or a different regex if insidious tabs are used instead of spaces...


1

Try this awk script: /CREATE/{ b=""; while (match($0, ";$")==0) { b=b $0"\n"; getline; } b=b $0; print b > "file1"; } /REPLACE/{ c=""; while (match($0, ";$")==0) { c=c $0"\n"; getline; } c=c $0; print c > "file2"; } So awk -f some.awk inputFile produces: cat file1 CREATE ...


1

Here's how I'd do it. First, for simplicity, let's list each of the chains individually, since I'm assuming you want to know which chain a rule belongs to: $ iptables -L INPUT --line-numbers Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT udp -- 109.224.241.0/24 0.0.0.0/0 udp dpt:5060 2 ...


1

Python Solution: #!/usr/bin/python3 #cycle.py old_server = 'avocado' new_servers = ['beet','banana','cherry','tomato'] replacement_count = 0 with open('example.txt') as file: #cycle through file for line in file: if old_server in line: replacement_count += 1 #increment counter to cycle through new servers #print ...


1

Pythonic way: >>> with open("/home/xieerqi/textfile.txt") as file: ... for line in file: ... if line.__contains__("foo"): ... VAR = line ... if line.__contains__("relevant=yes"): ... print VAR ... foo.bar.1 foo.bar.4 Put together in a script: DIR:/xieerqi skolodya@ubuntu:$ chmod +x relevance.py ...



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