Tag Info

New answers tagged

1

Since you asked about sed specifically, ls -ltr | sed '$!d'


1

With awk: ls -ltr | awk 'END { print }'


3

You're looking for tail : ls -ltr | tail -n 1 This will display only the last line of ls -ltr's output. You can control the number of lines by changing the value after -n; if you omit -n 1 entirely you'll get ten lines.


0

You can inspect it by, e.g.: awk 'BEGIN { printf "%s", ORS }' | od -c


1

Not familiar with the "YAML front matter" but as per this page if it's present, your file will always start with --- so you could run: sed -n '1{/^---$/!q};1,/^---$/{/^Title: \|^Place: /d};p' infile i.e. if the first line doesn't match --- quit, otherwise delete all lines matching those patterns if they occur between the 1st line and the next line ...


0

You wouldn't use Awk for this, but rather od (octal dump) For example, here is a simple text file created on a linux system with vi: lizard:~tmp$ cat foo This is a sentence. Here is the output of od: lizard:~tmp$ od -a foo 0000000 T h i s sp i s sp a sp s e n t e n 0000020 c e . nl nl 0000025 Notice that this file ...


0

In this case sed is the simplest. To add text to the last line of file f: sed '$s/$/ ddd/' f To append contents of a file ff after the last line of file f, one of: sed '$r ff' f cat ff >> f To append contents of a file ff (with only one line of data) to the last line of file f: sed '$s/$/ '$(< ff)'/' f


3

$ printf '%s\n' 10.0.{100..224}.\* 10.0.100.* 10.0.101.* 10.0.102.* 10.0.103.* 10.0.104.* 10.0.105.* [...snip...]


1

Try sed: $ sed -i 's/[0-9]\+$/\*/' file.txt 10.0.100.* 10.0.101.* 10.0.102.* EDIT: If you want to generate the range first and then replace the last octet try this: To generate the range you can use echo 10.0.{100..224}.0 | tr ' ' '\n' (John1024's answer shows another way using printf). Now to replace the last octet with * and save it to a file: $ ...


2

Here's the syntax to pass variables (and a few awk-style issues fixed): awk -v filename="${filename}" '$9 == filename { print "File exists" }'


0

In addition to heemayl's sed solution, also a grep option: grep -Ev '^Title:|^Place:' file.txt Explanation: grep -v to ignore lines that include Title: or Place: grep -E to stack the two terms ^ indicates the patterns should be at the beginning of the line


4

Here's one way of doing it in awk: $ printf "%s\n" "$data" | awk -F'\n' -v RS='(^|\n)#\n' '/./ {print $1}' first record, first field second record, first field# The trick is to set the record separator to either the beginning of the file (^), or a newline, followed by a # and another newline \n.


1

I know it's not quite what you're after, but personally - I don't get along with awk and so would suggest having a go in perl. Something like this: #!/usr/bin/perl #REALLY GOOD IDEA at the start of any perl code use strict; use warnings; #open some files for input open( my $exons, "<", 'exons.bed' ) or die $!; #record where our exons start and ...


2

The first issue is that you can't use bash variables inside awk like that. $a within awk evaluates to field a but a is empty since it is not defined in awk, but in bash. One way around this is to use awk's -v option to define the variable -v var=val --assign var=val Assign the value val to the variable var, before execution of the program ...


0

A sed code is simpler in this case, but since there's also an awk tag on this question: awk 'function readfile (file) { getline var < file ; return var } BEGIN { RS="^$" }; gsub(/PLACEHOLDER/,readfile("TestOutput.txt")) ' SQLInput.txt


2

try sed -i '/PLACEHOLDER/ r TestOutput.txt' SQLInput.txt where -i edit in place /PLACEHOLDER/ search for pattern r TestOutput.txt read file note that /PLACEHOLDER/ is not deleted. to have it deleted sed -i -e '/PLACEHOLDER/ r TestOutput.txt' -e s/PLACEHOLDER// SQLInput.txt where -e /PLACEHOLDER/d will delete entire line with PLACEHOLDER -e ...


2

You have various options... To pass shell variables to awk and use them in string comparison and let the shell create the file: awk -v chr="$chr" '$1==chr' "$infile" > "exons_${chr}.bed" To additionally let awk do the output into the file: awk -v chr="$chr" '$1==chr { print > "exons_" chr ".bed" }' "$infile"


4

I would do this in Perl: $ perl -lane '$k{"$F[0] $F[1]"}+=$F[2]+$F[3]; END{print "$_ $k{$_}" for keys(%k) }' file 2 1019 15 2 1021 4 2 1030 6 2 1031 8 2 1022 9 Or awk: awk '{a[$1" "$2]+=$3+$4}END{for (i in a){print i,a[i]}}' file If you want the output sorted according to the second column you could just pipe to sort: awk '{a[$1" ...


1

You could pre-sort the data and let awk handle the details: sort -n infile | awk 'NR>1 && p!=$2 {print p,s} {s+=$3+$4} {p=$2}' Output: 1019 15 1021 19 1022 28 1030 34 If you really want the keep the first column, do something like this: sort -n infile | awk 'NR>1 && p!=$1FS$2 {print p,s} {s+=$3+$4} {p=$1FS$2}' Output: 2 1019 ...


4

Maybe this could help, but is column 1 always 2 and does results depend on it? awk '{ map[$2] += $3 + $4; } END { for (i in map) { print "2", i, map[i] | "sort -t't'" } }' file or as mentioned by glenn jackman in comments about sorting: gawk '{ map[$2] += $3 + $4; } END { PROCINFO["sorted_in"] = "@ind_str_asc"; for (i in map) { print 2, i, map[i] } }' ...


0

Assuming that the operations need to be done on the second column (trim last 7 chars): [whatever command you use to open your file] | awk '{ print $2 }' | rev | cut -c 8- | rev Here's the breakdown: awk '{ print $2 }' Gets the second column of the file rev prepares the string (reverses it). I don't think cut can remove last N characters, so this thing ...


3

Since you have been using double quotes, any $-variables will be expanded by bash before they reach awk. That's the first problem: { print $1, $3 } will expand to { print , }. Next, the awk syntax you are trying to use is /<regular expression>/ { <action> }: the regular expression is not allowed to contain any unquoted slashes, but $PWD ...


1

Since you indicate that you are aware that this will break your XML, you can do what you want with one of these: Perl perl -ne 'print unless m#^<units>.*</Networks>$#' file.xml perl -ne 'm#^<units>.*</Networks>$# ? next : print' file.xml awk awk '!/^<units>.*<\/Networks>$/' file.xml sed sed ...


3

Do not use regular expressions to parse XML. It's an excellent way to create brittle code, because there's a bunch of perfectly valid things you can do with XML which will break a regex. Things like reformatting the XML in entirely valid ways (such as 'pretty printing' it in a nested/indented form) will break your code. Instead I would suggest - use an XML ...


2

Try this: perl -i -pe 's|^<units>.*</Networks>$||' /my/filename Note that if you have leading or trailing space in the line you will need this instead: perl -i -pe 's|^ *<units>.*</Networks> *$||' /my/filename I uesd pipe as a separator rather than slash to avoid unnecessary escaping.


-1

awk ' NR == 1 { a = $1 } NR == 2 { b = $1 } END { print a print b print b/a } ' data where data is a text file containing, 0250368000 0182885654


-1

I have used an old step by step trick to obtain something that works, but would have liked to do it all in one step. num1=$(awk 'NR==2' test1.txt) echo $num1 #result is 0247421924 num2=$(awk 'NR==1' test1.txt) echo $num2 #result is 0250368000 score=`awk 'BEGIN{printf("%0.0f", '$num1' * 100 / '$num2' )}'` echo $score # result is 99 As you can see, I had ...


1

quotient=$(dc -f test.txt -e 'r[num desired precision]k/p')


2

Assign to a variable var=$(awk 'NR==1{a=$0}NR==2{print $0/a}' test.txt) or output to a file: awk 'NR==1{a=$0}NR==2{print $0/a}' test.txt > output


0

sed 'x;$!d' <infile That should work for you.


3

How about tail -n2 myfile.csv | head -n1 | awk ....?


3

Based on a discussion we had in chat, what you want is to print all lines whose 1st and 4th fields are the same as the 1st and 4th fields of another line. If so, you can do: awk -F';' '{ if(NR==1){n=0; a[n]=$0} if($1==l1 && $4==l4){a[++n]=$0} else{ for (l in a){print a[l];} ...


0

@Archemar oh man, so stupid. That solved the problem. Deleted row "type" and "place" (fortunately I don't need them) and now it works ... thanks so much for taking a look


4

I've refactored your code a bit... for file in *.sam ; do awk -v basename="$(basename $file .sam)" ' { non = ($3 == "blah" && $4 >= 451000 && $4 <= 468999) ? "" : "_non" outfile = basename non "_ribos.sam" print > outfile } ' "$file" done


2

Assuming ordering of output is no requirement... awk ' BEGIN { FS=OFS=SUBSEP="," } { s4[$1,$2]+=$4 ; s5[$1,$2]+=$5 } END { for (k in s4) print k,s4[k],s5[k] } ' file1 ... fileN (Edited and restructured for better legibility.)


0

Here's a three-pronged approach: sed 's/^#/\n#/' file | perl -00lne '@A=split(/\n/); print shift(@A); print join "\n", reverse @A' | grep . Explanation The sed command adds a newline before each line that starts with a #. This enables us to use Perl's paragraph mode (see below). The Perl options: -00 activates "paragraph mode" a "line" ...


2

With sed: sed '/#Start para/{x;G;s/^\n//;p;s/.*//;x;d};G;s/\n$//;h;$!d' input.file For lines which consist #Start para append it to the end of hold space by exchange: pattern space <-> hold space then return data from hold appendinG it to pattern. (same can be done by H(append to hold) then return to pattern by g). If hold space was empty, pattern ...


2

For a more conservative, less cryptic approach... awk ' /^#/ { printf "%s",out ; print ; out = "" } /^[0-9]/ { out = $0 RS out } END { printf "%s",out } '


0

Use tac. Yes there is a command called tac which does reverse of cat


5

If the only # are the beginning of the block then with awk you can try $ awk -F'\n' 'BEGIN{RS="#"}NR>1{print "#"$1;for(i=NF-1;i>1;i--)print $i}' file Result: #Start para 3 you dq 2 welcomes dq 1 world dq 0 hello dq #Start para 2 you tq 1 are tq 0 how tq #Start para 4 world dq 3 the dq 2 to dq 1 hello dq 0 say dq


1

As requested by @KM, here is my answer. #! /bin/sh # this script pulls all rows from a log that are directly or # indirectly related to a given session id. Session IDs are stored # in $2 of each row. This field may be null. Directly related # rows are those with $2 matching the supplied parameter. Indirectly # related rows are those with $3 (aka xid) ...


0

awk ' /^Event:/{ d=substr($0,match($0,"[0-9]{4}(/[0-9]{2}){2}"),RLENGTH) "date +\"%Y/%m/%d\" -d \""d" 30 days\""|getline ds sub(d,ds) } 1' file For lines which starts from Event: find substring matching date format and put in in variable d. Then use system date command to set variable ds as date more than d for 30 days and substitute d ...


0

Any answer will depend on what you can actually know about your input. If you know you want the second bar3 surrounded by <font>, you could do: perl -pe 's#(.*bar3.*)(bar3)#$1<font style=BACKGROUND-COLOR:red>$2</font>#' file or sed 's#\(.*bar3.*\)\(bar3\)#\1<font style=BACKGROUND-COLOR:red>\2</font>#' file or, with GNU ...


1

With perl and the Date::Manip module, you could do: perl -MDate::Manip -pi -e 's|^Event.*?"\K\d{4}/\d\d/\d\d| UnixDate(DateCalc("$&", "30 days later"), "%Y/%m/%d")|e' file1 file2...


1

A switch/case statement is a non-standard extension. If you use GNU awk the syntax for your application case is case /AlarmLevel\.[0-9]+/ :


0

This is a bit of code snippit that should do what your asking, though it seems to me there a logical problem is the question, since the output of this loop is identical with or without the second test since the match will occur either time. I'm guessing you need looking to do a more complex test in the second run of awk than you described. What this ...


3

awk '{ print ">"$2 ; while (length($1)) { print substr($1,1,15) ; $1=substr($1,16) } }'


5

Here are a couple of ways: Perl perl -ane '$F[0]=~s/.{15}/$&\n/g; print ">$F[1]\n$F[0]\n"' file awk awk '{i=0; printf ">%s\n",$2; while(i<=length($1)){ printf "%s\n", substr($1,i,15);i+=15 }}' file If you really want to use fold within awk, you could do awk '{printf ">%s\n",$2; system("echo " $1 "| fold -w ...


3

With python test.py < input and test.py: import sys for i in sys.stdin: s, ident = i.rstrip().split() print '>{0}'.format(ident) while s: print s[:15] s = s[15:]


0

Here is a Python solution: #!/usr/bin/env python2 with open('file.txt') as f: for line in f: if line.startswith('IMS00'): print line.replace('Jan14', 'Jan15') elif line.startswith('IMS01'): print line.replace('Feb14', 'Feb15') This script will read each line from file, then it will check if the line starts with ...



Top 50 recent answers are included