Tag Info

New answers tagged

2

With zsh: setopt extendedglob # best in ~/.zshrc extract_numbers() REPLY=${(SM)REPLY##<->-<->} for file (*<->-<->*.dat(no+extract_numbers)) { do-what-you-will-with $file } We define a sorting function (extract_numbers) that returns the Substring of the file name that Matches <->-<-> (<-> matching any positive ...


3

You could also handle this with bash parameter expansion: s=swp-RedHat-Linux-OS-5.5.0.0-03 echo ${s%-*} ${s##*-} Output: swp-RedHat-Linux-OS-5.5.0.0 03


0

This command will give the filename of only one file sorted by the numeric fields, assuming the pattern is Sample_nnnnnnnnnn-ppp_xyz.dat: ls Sample_$(ls -l *.dat | awk -F ' |_' {'print $10'} |sort -n| head -1)*.dat


1

You can just do a simple sort. Assuming your file names contain no whitespace or other strange characters, this command will always give you the oldest file: find . -name '*dat' | sort -r | tail -n1 If you need to deal with strange characters, use find . -name '*dat' -print0 | sort -zr | tr '\0' '\n' | tail -n1 So, you could do something along the ...


1

assuming all files do not begin with Sample numeric field are sperated by _ you can't relay on file time I would use sort -t_ -k2n which yield for the four files given in sample Sample_0000137294-003_xyz.dat Sample_0000237826-001_xyz.dat Sample_0000237826-002_xyz.dat Sample_0000437564-001_xyz.dat


1

#!/bin/bash ls /tmp/*dat > filelist while read fn do #do something with /tmp/$fn done < filelist


11

You can do it with single command with sed 's/\(.*\)-/\1 /' The point is that sed is very greedy, so matches as many characters before - as possible, including other -. $ echo 'swp-RedHat-Linux-OS-5.5.0.0-03' | sed 's/\(.*\)-/\1 /' swp-RedHat-Linux-OS-5.5.0.0 03


5

Something like this worked for me, although I'm sure there are better ways echo "swp-RedHat-Linux-OS-5.5.0.0-03" | rev | sed 's/-/ /' | rev swp-RedHat-Linux-OS-5.5.0.0 03


1

The newline for sed's append is break it, so you have to backslash newline or sustitute by \n symbol: sed -i '' '/user_pref("mail.identity.id1.reply_on_top", 1);/ a\ user_pref("mail.identity.id1.sig_file", "/Users/illias.seba/Library/Mail/V2/MailData/Signatures/signature.html");\ user_pref("mail.identity.id1.sig_file-rel", ...


1

You should do: { <infile tr \} \\n;echo {; } | paste -d'}\n' - /dev/null >outfile It's probably the most efficient solution. That puts a {} to protect any possible trailing data. With one more tr process you can swap that around and do a blank line at the head of the first { field. Like... tr {} '}\n'| paste -d{\\0 /dev/null - | tr {}\\n \\n{} ...


3

Perl to the rescue: perl -i~ -e ' $/ = \1024; while (<>) { print "\n" if $closing and /^{/; undef $closing; s/}{/}\n{/g; print; $closing = 1 if /}$/; } ' input1 input2 Setting $/ to \1024 will read the file in chunks of 1024 bytes. ...


4

You can use another tool that lets you set the input record separator. For example Perl perl -pe 'BEGIN{ $/="}{" } s/}{/}\n{/g' file The special variable $/ is the input record separator. Setting it to }{ defines lines as ending in }{. That way you can achieve what you want without reading the entire thing into memory. mawk or gawk awk -v RS="}{" ...


1

You are making your life more complicated than necessary ;-) If the subshell command (the expression inside your $()) already provides a list of filenames, and the "target command" (rm -rf in your case) already accepts a list of filenames, why not just use git status --porcelain| sed 's/^??//g' | xargs rm -rf Or, if you wish to practice a "for loop" in ...


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


0

With GNU sed you can do: sed -E 's/(\|[^ |]+) /\1| / s/(([^|]* )?([^|]*))\|(([^ ]*)(.*))/\1\6\n\2\4/ /\n/P;D' <infile ...which prints... TMPRSS2 AADAT Sample1 pp9284 AADAT Sample1 ERG TMPRSS2 Sample2 ERG pp9284 Sample2 TMPRSS2 ETV1 Sample3 pp9284 ETV1 Sample3 PDE4A MIA Sample4 PDE4A MIA-RAB4B Sample4 PDE4A RAB4B Sample4 ...


4

With bash: # important to use parentheses, not braces, to localize changes to IFS # the variable is purposefully unquoted split_pipe() ( IFS='|'; echo $1 ) while read -r first second third; do for word1 in $(split_pipe "$first"); do for word2 in $(split_pipe "$second"); do echo $word1 $word2 $third done done done < file


4

$ awk '{split($1,a,"|"); split($2,b,"|"); for (i in a) {for (j in b) print a[i],b[j];}}' file TMPRSS2 AADAT pp9284 AADAT ERG TMPRSS2 ERG pp9284 TMPRSS2 ETV1 pp9284 ETV1 PDE4A RAB4B-EGLN2 PDE4A EGLN2 PDE4A MIA PDE4A MIA-RAB4B PDE4A RAB4B To also print the third (SampleN) field, you can just add $3 to the print statement inside the loops, i.e. $ awk ...


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 ...


1

Here are some approaches. You haven't explained what you want to do with the string you want to extract so I will just print it. awk with multiple delimiters echo Sample_0000860156-001-out_20150224150524.xml.gz | awk -F '[_-]' '{print $2"-"$3}' GNU grep echo Sample_0000860156-001-out_20150224150524.xml.gz | grep -oP '_\K.*(?=-)' sed echo ...


0

Using GNU awk with match awk 'match($0,/_([0-9]+-[0-9]+)/,a){print "file."a[1]".gz"}' <<< "$FILENAME" Ouptut file.0000860156-001.gz


1

This will create a new empty file for every *.gz on the directory that keeps the pattern of the example: touch $(ls *.gz | awk -F '_' {'print $2'} | awk -F "-out" {'print $1'}) or as suggested by Jidder : touch $(ls *.gz | awk -F '_|-out' {'print $2'})


1

You need a / delimiter ending the regexp before the comma. And you need to put quotes around the entire expression argument to sed because you have spaces in both regexps. sed -e "/pd begin $(echo 3:1I:1:1|sed 0,/:/{s/:/' '/})/,/pd end $(echo 3:1I:1:1|sed 0,/:/{s/:/' '/})/p" /tmp/hp-raid-data-harvester.out ...


0

I don't know if it acceptable, but you can remove everything before the second pipe |, and everything after the first space minus 2 characters : sed -e 's/^[^|]*|/>/' -e 's/^[^|]*|/>/' -e 's/.. .*$//'


0

Through python3 #!/usr/bin/python3 import csv import sys file1, file2 = sys.argv[1], sys.argv[2] with open(file2) as second, open(file1) as first: second_list = second.readlines() first_list = first.readlines() for line1 in first_list: for line2 in second_list: if line1.split(':')[1] == line2.split(':')[1]: ...


2

With sed you can probably do: sed 's|[^:]*:\([^:]*\).*|/^[^:]*:\1:/s/$/:&/;t|' file2 | sed -f - file1 ...which would involve one sed process reading the second file and writing a sed script for editing the first into a second sed's stdin. As near as I can tell you shouldn't have any problem with directly injecting the contents verbatim into a regexp ...


8

This sounds like a task for join: join -t":" -o "1.1,1.2,1.3,1.4,1.5,2.1,2.2,2.3" \ -j 2 <(sort -k2,2 -t: test1) <(sort -k2,2 -t: test2) Output: Julian:Brude:Other:Other:Other:Jb:Brude:kemin Robert:Dillain:Other:Other:Other:R:Dillain:bodent Megan:Flikk:Other:Other:Other:Mb:Flikk:kentin Jesus:Kimmel:Other:Other:Other:Jbb:Kimmel:verlin ...


5

This is simple task for awk: awk -F':' -vOFS=':' 'NR==FNR{a[$2]=$0;next}{print $0,a[$2]}' file2 file1 First we set : as field separator both for input (with -F) and output (with OFS) then if first file is processed (file2) we assign whole line to table element indexed with second field. When next next file (file1) is processed we print its lines adding ...


0

One way to do this could involve unexpand. The description for it and the expand utility can be found here: The unexpand utility shall copy files or standard input to standard output, converting <blank> characters at the beginning of each line into the maximum number of <tab> characters followed by the minimum number of <space> characters ...


0

Liked @Costas suggestion, and another option is: gawk ' { f1=substr($0,2,2) f2=substr($0,4,36) gsub(/ *$/, "", f2) print f1 " " f2 } '


1

If the columns are separated by tabs you can specify the tab character as the field separator. This will prevent the default behavior of awk to treat spaces as separate columns. cat <data file> | awk -F"\t" '{print $1, $2}' root@ubuntu32:/tmp# cat testtext | awk -F"\t" '{print $1, $2}' 16 SQL*Plus 16 TOAD background query session


0

Try this: find /path/to/the/directory -type f -exec sed -i 's/include\(.*\)/require_once\1/' {} + This will find all the files in the given directory and replace the lines "include("path/to/file.php");" of each file to "require_once("path/to/file.php");".


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

You could try using sed : sed -rn ':a;/^([^~]*~){13}[^~]*$/!{N;s/\n//;ba};p' yourfile.tsv What it does The script has three parts separated by a ;: :a defines a label we can branch to /^([^~]*~){13}[^~]*$/!{N;s/\n//;ba} searches for a complete column /^([^~]*~){13}[^~]*$/ looks for a line with exactly 14 fields (0 or more occurrences of anything that ...


0

You don't need external languages, a few standard commands can do the trick. For example, to insert something at line 5 of origin.txt you could do: tmpfile=`mktemp` head -n4 origin.txt > $tmpfile echo "I'm line 5 now" >> $tmpfile tail -n +5 origin.txt >> $tmpfile mv $tmpfile origin.txt


0

sed "s/\($(printf "[0-9][0-9]%s" '' - - '\) \(' : : '')\)/\1T\2/g" Maybe try building out your pattern programmatically. That can sometimes make it a little easier to nail down the very specific types. The above expands to... sed 's/\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\) \([0-9][0-9]:[0-9][0-9]:[0-9][0-9]\)/\1T\2/g' (but without the ...


3

Try: for f in ./*.txt; do sed -i '' -e "s/$/$(printf '\t')$f/g" "$f" done


0

sed -e '$!N;$b' -e "/\n.*\n/!N;s/$old/$new/;t" -e P\;D The above will work, but here's the catch: newlines in $old need to be represented as \n and newlines in $new should be preceded by a \backslash. The newlines in your values are what gave you trouble. For example, after expansion: sed 's/24.0000000001 .000000000000 .0000000000/...' ...doesn't work. ...


-1

sed s/"$old"/"$new"/g works for me


0

First, let's define a sample string for new: $ new=$'1 2 3\n4 5 6\n7 8 9' Now, let's remove the three lines that follow primitive lattice vectors and replace them with new: $ awk -v "new=$new" 'f==1{print new} f{f--;next} {print} /primitive/{f=3}' input scale 1.000 primitive lattice vectors 1 2 3 4 5 6 7 8 9 grid dimensions 100 100 100


0

sed -e "/$old/i$new" -e "/$old/{N;N;d}" input > output where $old can be just 28.47969107 or ONE first line 28.47969107 0.00000000 0.00000000 $new SHOULD be in form 28.47969107 0.00000000 0.00000000\n0.00000000 28.47969107 0.00000000\n0.00000000 0.00000000 28.47969107 Other way (if you have new GNU sed (version >4.2.1) with -z option) to escape ...


1

As @Costas pointed out, you are using globbing patterns whereas sed uses BRE regular expressions (or ERE if using sed's -r option). Your command should resemble something like this : sed -i -r 's/-[0-9]+x[0-9]+\.jpg/.jpg/g' myfile.sql Explanation : [0-9] means "any number" (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) + means "one or more of the previous atom" (The ...


0

This makes the same assumption about spaces as @Gilles answer does, but it eschews the while...read loop. It first backslash escapes any occurrence of any of sed's BRE metacharacters, then prints its current line number, then globally substitutes every pair of not-space characters it can find into a working sed substitution statement. Next in the pipeline a ...


1

To print lines from 2 to 4: sed '2,4!d' file To print line 2, and additionally 2 lines following it: sed '2,+2!d' file


1

If you mean you want to print a range of lines then you can use this sed -n 2,4p somefile.txt. This will print lines 2 to 4 from somefile.txt.


1

With GNU sed, you can print lines 2, 3, 10, etc., using: sed -n '2p;10p;3p;...'


2

I strongly recommend the use of either of the programs named html2text (1) (2) instead. Parsing HTML is much harder than it looks.


6

If you are not insisting on sed, the best thing to do this would be lynx. lynx --dump <filename>.html This will output the content of the html file in the format the html code was intending to display. The only condition is that the filename should have a .html or .htm extension.


2

As long as your HTML tags are confined to a single line, the following will work: sed 's/<[^>]*>//g'



Top 50 recent answers are included