Tag Info

New answers tagged

1

Time comparation (not an answer) The efficiency of the answers is not important. Nevertheless, following @josephwb approach, I tried to time all the answers presented. I use as input the Portuguese translation of Victor Hugo "Les Miserables" (great book!) and count the occurrences of "a". $ wc miseraveis.txt 29331 304166 1852674 miseraveis.txt In the ...


0

A basic approach with read and sed (and no error checking): #!/usr/bin/env bash read -p "Enter Client ID: " client read -p "Enter field to change: " field read -p "Enter new data: " data case "$field" in *name) field=2 ;; address) field=3 ;; town) field=4 ;; esac sed -i "/^$client/ {s/[^,]*/$data/$field}" datafile


0

I see at least one mistake that could explain the problem. I haven't reviewed your script in detail. Look at what this line does: for line in `cat ${PARAMETERSFILE}` Take the value of the variable PARAMETERSFILE. Split that value at whitespace — this results in a single word if the value contains only “ordinary” characters. Perform filename expansion ...


1

replace a by the char to be counted. Output is the counter for each line. perl -nE 'say y!a!!'


1

Here is a simple Python script to find the count of " in each line of a file: #!/usr/bin/env python2 with open('file.txt') as f: for line in f: print line.count('"') Here we have used the count method of built-in str type.


3

To use awk builtin's: awk -F, -v field="Flag10" '{sub(field",.*",field);print split($0,any)}' doc.file The script will remove everything after the first occurrence of the value of field (in this case, Flag10) and then print the result of splitting the line on commas. Since we've deleted everything after Flag10, this results in the number of fields left ...


2

I typically create an associative array, mapping the header names to the column numbers awk -F, -v head="Flag10" ' FNR == 1 {for (i=1; i<=NF; i++) col[$i] = i} {print $col[head]} ' << END ,Flag2,,Flag4,Flag5,,,,,Flag10,Flag11,Flag12,Flag13 10,20,30,40,50,60,70,80,90,100,110,120,130 END Flag10 100


3

You could do like below, awk -F, '{for(i=1;i<=NF;i++)if($i=="Flag10")print i}' file Example: $ echo ',Flag2,,Flag4,Flag5,,,,,Flag10,Flag11,Flag12,Flag13' | awk -F, '{for(i=1;i<=NF;i++)if($i=="Flag10")print i}' 10


1

Assuming your data is as simple as you show, you could do awk -F, -v OFS=, '{k=$3; $3="\"new text\""; $4=k}1' file Or, perl -F, -lane 's/$F[1]/$F[1],"new text"/; print' file However, this will fail with data that can contain nested commas like: "foo", "bar,baz", "bar" The above is a valid csv file but these solutions will treat it as having 4 ...


2

To do not involve some heavy instruments (like sed or awk): paste -d',' <(cut -d',' -f-2 file) column.file <(cut -d',' -f3- file) This assumes that the column you want to add is saved in the file column.file.


0

If you can afford to install external tools, than I would recommend Augeas - this is the only tool for working with config files you will ever need. It represents configs as a tree. Read more here.


1

You could use a perl oneliner for that: $ cat file a b c $ perl -i -wne 'if(/b/){print STDOUT $_}else{print}' file b $ cat file a c


1

Did you try something like: grep -Eri -l "drucken" app/views | xargs sed -e '/drucken/d' -i where '-l' tells grep to only print the file name, '-i' tells sed to modify that file on the fly. Alternatively you could loop with sed over all files, but it will "touch" all of them even if the file doesn't contain the requested word: find app/views -type f ...


1

Looks to me like you're looking for grep -v? -v, --invert-match Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX.) So grep -v 'drucken' file1 > file2 will give you a file2 with those lines removed. Alternatively and untried, something along the lines of sed -e '/drucken/d' infile > ...


3

Why are you using grep to get stuff out of xml-files? You're using grep, so you're probably on a Linux or BSD system, so why not just use xpath from the command line instead? xpath -q -e "SSID/essid/text()" /path/to/file.xml Will do precisely what you want, and with far less hassle than a regular expression which tend to be rather fragile in getting stuff ...


2

With GNU grep: grep -oP 'essid.*>\K.*(?=<)' file Output: WLAN-123651234


4

With GNU grep linked to a recent version of the PCRE library (Perl Compatible Regular Expressions), you could try: $ grep -oP '<essid\b[^<>]*>\K[^<>]*(?=</essid>)' file WLAN-123651234 This would extract the contents of essid tag. Explanation: <essid matches exactly the string <essid \b called word boundary which matches ...


1

You can do it with xargs + sh -c: <file1.txt xargs -d'\n' -L1 sh -c 'mkdir -p -- "$0" printf %s\\n "function $0() { return stuff; }" >"$0"/function.js' not using the -I {} construct to avoid expansion if your text file contains funky stuff (e.g. lines like - $'\n' \ $PATH).


0

Non-sed solution using q: $ q -d, -H -O 'select [student-id],first,last,hwk1,hwk2,hwk3,exam1,hwk4,hwk5,exam2 from sample.csv' student-id,first,last,hwk1,hwk2,hwk3,exam1,hwk4,hwk5,exam2 pts-avail,,,100,150,100,200,150,100,300 991-78-7872,Ken,Thompson,95,143,79,185,135,95,259


2

Just loop over all lines in the file: while read line do echo "function $line() { return stuff; }" > "$line/function.js" done < foo.txt Of course this assumes you have already directories named as lines in the foo.txt. If this is not the case then first create them with mkdir "$line". Another approach, with awk instead of loop would be: awk ...


2

To collect the two lines separated by a newline in a variable... while read line1 && read line2 do bothlines=$line1$'\n'$line2 do_something_with "$bothlines" done < test.fa (works with ksh, bash, zsh)


0

Try the following command line: $ cat file.txt | sed 's@+@ @g;s@%@\\x@g' | xargs -0 printf "%b" You may define it as alias and add it to your shell rc files: $ alias urldecode='sed "s@+@ @g;s@%@\\\\x@g" | xargs -0 printf "%b"' Then every time when you need it, simply go with: $ echo "http%3A%2F%2Fwww" | urldecode http://www When scripting, you ...


3

Another approach: awk 'BEGIN{FS=":|\n";RS=""}NR==1{print $1,$3,$5,$7,$9}{print $2,$4,$6,$8,$10}'


1

You may want to put the header manually and run the following awk on the file: awk -F: '{count++; printf("%s ",$2); if (count>5){count=0; printf("\n");}}' x.txt >> res.txt Assuming x.txt contains the inputs as you have and res.txt is a prepared file with headers as follows: name sub branch DOB company The resulting output will be: name sub ...


1

I was in the process of writing the following answer to the other, related question here when it was deleted. I dunno if you'll be able to follow that link, but (an abbreviation of) the example data referred to below is this: Sending PINGREQ Received PINGRESP Sending PINGREQ Received PINGRESP Received PUBLISH (d0, q0, r0, m0, 'm/gf/TMX6BP/075/d/SVlts', ... ...


7

On a GNU system you'll need to use sed or similar if your locale uses multibyte characters (as jimmij suggests) because GNU tr can only reference a character per byte. In an ASCII locale you can remove all duplicates w/ tr like: LC_ALL=C tr -s '\0-\255' <input So... echo Thhiisss iisss mmyyy nameeee| LC_ALL=C tr -s '\0-\255' ...prints... This is my ...


12

With tr: echo "Thhiisss iisss mmyyy nameeee" | tr -s 'a-z' Explanation: The -s switch of tr "squeezes" repeat characters. As shown, the switch can be used with a range of characters: a to z.


2

Try tr: echo "Thhiisss iisss mmyyy nameeee" | tr -s 'hismye'


6

One way with sed: sed ':X;s/\(.\)\1/\1/g;tX' or even simpler: sed 's/\(.\)\1*/\1/g' (thanks Costas and mikeserv for comments).


2

You can say: $ awk '($4>7 && $4!=swap) {swap=$4; print $1, $4}' file 00:20:02 7.3 00:45:02 10.5 01:05:02 13.5 That is, store the last value of SWAP bigger than 7 in the swap variable and keep comparing the value. You can get a nicer output by setting the output field separator to tab: -v OFS="\t" or BEGIN {OFS="\t"}. Or you can also pipe to ...


0

Sounds like you want something like: awk 'BEGIN {RS="\n\n"} /What exactly is Free/' br.txt This should print the entire paragraphs where the text is found (RS="\n\n").


1

Well, rather confusing but anyway... Judging by the output of mosquitto_sub -d -t +/# 2> >(grep PUBLISH) your app seems to output to both stderr and stdout (otherwise you should only get lines matching PUBLISH in your output). It prints the debug messages (Sending... and Received...) to stderr and the actual data (810,5440995,6143...) to stdout. ...


0

I simulated mosquitto in bash/ksh environment and found stub_mosquitto | sed -e 's/.*m0, //' -e 's/, .*bytes))//' | while read line; do if [[ "${line}" = \'* ]]; then echo -n "${line}, " else echo "${line}, $(date)" fi done I used sed here for cutting the interesting parts. I use while read line for a simple way ...


1

If you use some text editor try to use just one: awk -F ".scheduleName." ' NR==1{printf "%s ", FILENAME} NR<4{printf "%s ",$2} END{print""}' $(ls -t | head -1) I hope is there more than 1 scheduleName in line to remove first and last symbols from second field?


2

Maybe by storing the output in variables and writing these variables to the file? output1=`ls -tr | tail -n1` output2=`head -n3 bla-1357135486.xml | awk -F 'scheduleName' '{ print $2 }' | sed -r 's/^.{1}//' | sed -r 's/.{1}$//'` echo "$output1 $output2" > /tmp/output.file


1

How I understand OP there are some blocks of text which separated by empty lines and OP wants to remove every duplicates: awk -v RS='\n\n' -v ORS="\n\n" '!seen[$0]++' file If OP wants to just remove the block try it via GNU sed: sed -z 's~recursive-test yes;\ntest-limit{\ntests 10;\n};\nlocation "testLoc" {\ntype test;\n};\nlocation "testLoc2"{\ntype ...


1

< input python -c 'import sys; sys.stdout.write(sys.stdin.read().replace("""recursive-test yes;\ntest-limit{\ntests 10;\n};\nlocation "testLoc" {\ntype test;\n};\nlocation "testLoc2"{\ntype test;\nfile "/etc/var/test.sql";\n};\ninclude "/etc/var/test.conf";\n};""", ""))' python's triple quotes (""") nicely helps in not having to escape quotes in the ...


1

Sed or awk are one-line editors. You have to join two lines in one or use options (exist in GNU sed versions > 4.2.1) sed -zi 's~Assurez-vous de bien recevoir tous nos messages en ajoutant bebeco@cab05\.net a votre carnet d'\''adresses.<br />\n Si vous avez des difficult\\&eacute;s pour visualiser ce message, rendez-vous sur~[{headerbebeco}]~g' ...


2

You can drop-ship text from the cut buffer with swap-pasting -- pasting into a selection swaps, so dwVP line-deletes everything but the deleted word. Start with Use three words. This is the first string of another block of strings. This is the second string of another block of strings. This is the third string of another block of strings. and do ...


0

I would start like you do by splitting the sentence up into new lines but after that I would Number each line for both blocks Paste one part completely to the other sort on number remove the numbers This could look like this Blok1 - :%s/\s/\r/g Blok1 - %s/^/\=line('.')*1000+1 Blok2 - %s/^/\=line('.')*1000 Copy entire Blok2 into buffer of Blok1 sort n ...


3

The awk solution to round up the list of alternatives: awk -v RS=', ' -F'=' '$1=="Foo"' <file> Treat each record to be delimited by ', ', and split each record into fields on the = character (using -F) as well. Then it's just a matter of matching on the first field $1. The suggestion shown here is a simple string matching, feel free to use regexes, ...


1

If you don't have a grep available with the -o option, this ought to do the trick as well: sed -e 's/, /\n/g' | grep '^Foo=' That's using sed to replace every comma+space with a newline (breaking each key-value pair onto its own line), and then grep to search for only the 'Foo' key. Test case: printf "%s\n" "Foo=1, Bar=2, Baz=3" "Bar=4, Foo=2, Baz=3" ...


1

Given your example, a brittle solution could involve cut: tr ', ' '[\n*]' <input | cut -sd F -f1- ...which would put each key/value pair on a separate line by transforming the intervening commas and spaces into \newlines, and then cutting out lines which don't contain an F. But that is a highly specialized example, and can only work if you can be sure ...


1

Depending on the complexity of your real-world situation, this sed command may be sufficient: sed -n 's/^.*\(\<Foo=[^,]*\).*/\1/p' Here's the worked example FIELD='Foo' sed -n "s/^.*\(\<${FIELD}=[^,]*\).*/\1/p' << xxEOFxx Foo=1, Bar=2, Baz=3 Bar=4, Foo=2, Baz=3 Bar=42, Baz=42, Foo=3 xxEOFxx Foo=1 Foo=2 Foo=3


5

grep can do this with -o option: grep -o 'Foo=[^,]*' file


0

With tr and cut: tr ' |' '>>' <input |cut -d\> -f1,6 ...which transforms all spaces and |pipes into > right-angles, then cuts out from among the resultant > -delimited -fields only the 1st and 6th. With sed... sed -n 'y/ /\n/;s/.*|/>/;P' <input Which also y/// transforms all spaces into \newlines, then s///ubstitutes the ...


0

This can also be done using cut: input=">>tr|G1PEZ0|G1PEZ0_MYOLU Uncharacterized protein (Fragment) OS=Myotis lucifugus GN=SNCG PE=4 SV=1" echo "$input" | cut -d" " -f1 | cut -d"|" -f3 | sed "s/^/>/"


0

How about this: str='>>tr|G1PEZ0|G1PEZ0_MYOLU Uncharacterized protein (Fragment) OS=Myotis lucifugus GN=SNCG PE=4 SV=1' echo "$str" | sed -e 's/ .*//' -e 's/.*|//' -e 's/^/>/' That uses 3 separate substitutions - delete everything from the 1st space on the line onwards, delete everything up to the last | on the line, and put a > back as the ...


0

There is an awk command as following: awk 'NR!=1{print RS$0 >"file"i++}' RS='SASN2010Aber.CallEventRecord.egsnPDPRecord' infile The NR!=1 skips the first record which is empty. RS='...' defines SASN2010Aber.CallEventRecord.egsnPDPRecord as Record Separator And the block print RS$0 >"file"i++ saves the each record($0) into 3 separates file with ...


0

Try: $ cat test01 | stdbuf -oL -eL awk -F',' '{print $2","$3 >> "data"$1".csv"; fflush("")}' In gawk 4.0.1, calling fflush() without any argument only flush stdout. You need calling fflush() with empty string "" to make awk flushes all open output files and pipes as well. With gawk 4.0.2 and later, if there is no argument, or if the argument ...



Top 50 recent answers are included