Tag Info

New answers tagged

3

html-xml-utils xml2 xml-twig-tools xmlstarlet


0

You could use perl: echo "a&a" | perl -MHTML::Entities -e 'while (<>) {print encode_entities($_, "&");}' Gives: a&amp;a As second parameter, you can specify witch special characters you want to encode.


0

if you search for more common solution (UNIX/Linux) you can use something like: GROUP=$(id |awk 'BEGIN { FS = "[=(]" } ; { print $4 }')


14

id also accepts paramters, so you don't have to grep for it (-g to print only the group, and -n to print names instead of ids): $ id -gn usera groupa To save that into a variable use that: groupname=$(id -gn usera)


0

Here is one-liner suggestion: pr -mt \ <(grep -o ".*: .*," in.json | grep -iw InstanceId | cut -d: -f2) \ <(grep -o ".*: .*," in.json | grep -iw Value | cut -d: -f2) \ <(grep -o ".*: .*," in.json | grep -iw Key | cut -d: -f2) Not perfect, but it'd work if you tweak it a bit. It basically using pr to print each set result per ...


4

The main problem has already been highlighted by don_crissti in his comment. He determined the file type by running: file koran ... which outputs: ASCII C++ program text, with CRLF, LF line terminators You need to remove the CR (Carriage Return - hex value \x0D) characters which are present at the end of some lines in your input file. This CR is ...


0

sed -E ' s/[0-9.+-]*e[-+]?[0-9]{2}/ &/g s/ *(.{22}[0-9])/\1/g' < input.txt > output.txt That assumes GNU or FreeBSD (or derivative like OS/X) sed, or a sed conformant to the next version of the Unix/POSIX standard (for -E).


3

Many grep implementation will use line buffered when standard output is terminal. When standard output is terminal, it's often an interactive session, you want to get data as soon as possible. So grep will write data to standard output as soon as seeing a newline. When standard output isn't terminal (often meaning non-interactive session), grep will use ...


0

for f in ???.txt do awk -f program.awk "$f" > "output$f" done will process all files whose names are three characters (any characters) followed by .txt.  To restrict it to only files whose names are three digits followed by .txt, use for f in [0-9][0-9][0-9].txt


0

That worked for me find docs/ -type f -iname "*.html" -exec \ perl -i.bak -0pe 's/_uacct = "UA-XXXXXX-X";\nurchinTracker\(\);/test/igs' {} \; Notice that -i.bak makes a copy of the original file with extension .bak, in case anything goes wrong. The replacement is now the string test. Replace that with your long replacement. With your huge replacement it ...


0

You can't escape single quotes inside single quotes. The easiest way for you is to put your script in a file and then run find arts/ -type f -iname "bass.html" -exec sed -i -f your-script.sed '{}' \;


1

You can do it like this: root@localhost:~# cat test gcc-4.8.3-1.aix7.1.ppc.rpm gcc-c++-4.8.3-1.aix7.1.ppc.rpm gcc-cpp-4.8.3-1.aix7.1.ppc.rpm gcc-gfortran-4.8.3-1.aix7.1.ppc.rpm libgcc-4.8.3-1.aix7.1.ppc.rpm root@localhost:~# awk 'BEGIN{FS=".aix7.1"} {print $1}' test gcc-4.8.3-1 gcc-c++-4.8.3-1 gcc-cpp-4.8.3-1 gcc-gfortran-4.8.3-1 libgcc-4.8.3-1 ...


5

I would split the line on dots and remove the last 4 fields awk -F. -v OFS=. '{NF-=4}1' But Costas's code in his comment more directly answers the requirements.


1

Something like this will work awk -F" *| *" 'NR>1{a[$1]+=$NF}END{for(i in a)print i,a[i]}' file Output queue3 61 queue4 48 queue5 29 queue6 48 queue1 39 queue2 235 or if they are in order like your example awk -F" *| *" 'NR==1{next}t!=$1{print t,x;x=""}{x+=$NF;t=$1}END{print t,x}' file Output queue1 39 queue2 235 queue3 61 queue4 48 queue5 29 ...


4

Use either of the two patterns: NR>6 { this_code_is_active } or this: NR<=6 { next } { this_code_is_active } Use FNR instead of NR if you have many files as arguments to awk and want to skip 6 lines in every file.


1

Try: awk 'FNR > 6 { #process here }' file


2

If you really want to do this in plain sed/awk, it is indeed possible: As mentioned by Joe, using SPACE as field separator & data value is a problem in awk. That is why I suggest using sed to re-format the data first: sed 's/ *$//' removes SPACEs in the end of the line (all but the first of your input lines end in SPACE, so this standardizes the input ...


1

perl -n0E 'say tr/0//, "+",tr/1//' or following OP tentative: awk -v RS=1 'END{print NR-1}'


1

Ed, man! !man ed: $ ed -s file <<EOT g/0/d n u g/1/d n EOT 13 1 17 0


1

Another possibility is to grep for lines matching exact "0" (or "1") and count the resulting lines: grep "^0$" filename | wc -l => 5 grep "^1$" filename | wc -l => 5 Or count both alternatives in a single grep: grep "^[01]$" filename | wc -l => 10


1

Delete everything but 0 and print the character counts: tr -cd 0 < file | wc -m Output: 5


4

This counts the number of ones and zeros in filename: $ sort <filename | uniq -c 5 0 5 1


3

With awk: awk '/0/{zero++} /1/{one++} END{printf "0: %d\n1: %d\n", zero, one}' filename With grep, needing two commands: grep -c 1 filename grep -c 0 filename For a string that covers the entire line: grep -cFx 'target string' filename Presumably your string might contain characters that have special meaning in regex, so we need to use -F. -x ...


0

I assume that you want to use awk to process ls -l for the additional information that ls -l provides. In general find and stat gives a more reproducible result, but feel free to use ls. One of the nice things that ls -l does, is it has a fixed width, therefore you can use substr for the start of line, then not specify the length, which will include all ...


0

This is intended to replace all of the while loop in your script: awk '{print substr($0, 31)>("replay_message_" NR-1 ".txt")}' file How it works: print substr($0, 31) This prints all but the first thirty characters of the line. >("replay_message_" NR-1 ".txt") This sends what was printed to a file named after the line number. When the awk ...


2

If you know that the date format will always have six space-separated fields, you could use: cut -d ' ' -f 7- If you know that your time stamp always occupies 30 characters, you can use: cut -c 31- If you know that your time stamps end with a digit, followed by a colon, followed by a space, and that your data does not include this pattern, you can use: ...


0

If you mean to replace a string consisting of two double quotes, and also pass your variables using option -v, as in: awk -v val="${encryptedkeyValue}" '{ gsub(/<KEY>/,val) } 1'


0

Much easy it can be done with sed #!/bin/bash unset pattern for fn in * do pattern=${pattern:+${pattern}\\|}$f done sed -i "/$pattern/s/$/ Found/" some.txt


0

Problems in your script: SC2006 - Use $(..) instead of legacy `..`. SC2012 - Use find instead of ls to better handle non-alphanumeric filenames. SC1035 - You are missing a required space after the !. 1 !/bin/bash ^––SC1035 You are missing a required space after the !. 2 list=$(ls -l | awk 'NR > 1 { print $9;}') ^––SC2012 Use find ...


1

The code is very convoluted. For example >> list=$(ls -l | awk 'NR > 1 { print $9;}') >> for fn in $list Why do you do ls -l but then extract $9 (the filename - but note that this does only work if no whitespace is part of the filename); instead of just for fn in * Then you do some grep for line numbers and awk for field extraction ...


0

!/bin/bash list=$(ls -l | awk 'NR > 1 { print $9;}') for fn in list do echo $fn n=`grep -n "$fn" some.txt | awk -F":" '{ print $1;}'` # change this awk -F" " 'NR=='"$n"'{OFS=" "; $(NF+1)="Found";}1' some.txt>some.out mv some.out some.txt done !/bin/bash list=$(ls -l | awk 'NR > 1 { print $9;}') for fn in list; do # also decided to ...


2

Your input files have DOS \r\n line endings. Remove the carriage returns with the dos2unix command or with sed -i 's/\r$//'


0

The command paste separate entries with tabs, which are then interpreted as a variable number of spaces upon display. If your input is already padded with whitespace, you may try deleting tabs from the output of paste, with | tr -d '\t', or turning each tab into a single space,with| tr '\t' ' ' (on my system, the first can be achieved using paste -d '', but ...


2

With xargs/grep: xargs -n1 -I '{}' grep '{}' -A1 File2.txt <File1.txt Explanation: -n1: forces xargs to execute the command for every line -I '{}': set a placeholer grep '{}' -A1 File2.txt: the command to execute -A1: print also the line after the search pattern File2.txt: search trough File2.txt <File1.txt: input is File1.txt


2

A quick way to do this is using: while read amino_acid do grep -A1 ${amino_acid} File2.txt >> output.txt done < File1.txt


3

Everything what you need to loop through acids in File1.txt and find matched line in File2.txt + 1 line which easy done by grep for acid in $(sed 's/^\s*//' File1.txt) do grep -FA1 "$acid" File2.txt done > Output.txt But if you like awk: awk ' FNR!=NR{ print " [",$1,"]" print acids[$1] next } /\[/{ acid=$2 next } { ...


0

When you do not want to grep the info directly out of the logfile, you can use echo "${var}" | grep "kdump" You could also use a pipe: awk ...your_script | grep "kdump" When you want to grep more words, use grep -E awk ...your_script | grep -E "kdump|Diggy|other string"


-2

$cat d.txt //svn.server.address/repos/project/module1/branches/issue-001-name1 //svn.server.address/repos/project/module2/branches/issue-002-name2 //svn.server.address/repos/project/module3/branches/issue-003-name3 $awk 'BEGIN{FS="/"}{print "svn","co",$0,$8}' d.txt svn co //svn.server.address/repos/project/module1/branches/issue-001-name1 issue-001-name1 svn ...


0

awk -F: '/:/{prefix=$1;next}/./{print prefix "/" $0}' Note that is not a problem to have double / in path. But if you'd you can add awk -F: '/:/{sub("/$","",$1);prefix=$1;next}/./{print prefix "/" $0}' or awk -F: '/:/{prefix=$1;s="/";if(prefix~"/$")s="";next}/./{print prefix s $0}'


2

There is no need to mix many instruments. Task can be done by sed only sed '/^INFO\|^DEBUG\|^TRACE\|^ERROR/{ /Logger2/{ :1 N /\nINFO\|\nDEBUG\|\nTRACE\|\nERROR/!s/\n// $!t1 D } }' log.entry


1

Based on one answer at http://stackoverflow.com/questions/9605232/merge-two-lines-into-one this seems to fit the bill #!/usr/local/bin/bash PATTERN1='TRACE *'; PATTERN2='DEBUG *'; PATTERN3='INFO *'; PATTERN4='ERROR *'; LINEOUT="" while read line; do case $line in $PATTERN1) echo $LINEOUT LINEOUT="$line" ...


1

perl filter for multiline log records (record begin mark) Use the following perl script as a working prototype. Usage script_path regular_expression log_files e.g. script_path "line \d" log_file_1 log_file_2 #!/usr/bin/perl $pattern = qr/(?^s)$ARGV[0]/; shift; # process filtering expression # (?^s) - treats matched string as single line my $line = ''; # ...


3

There's already a solution based on paste in the comment above. But if you prefer awk, here's an awk-solution: awk 'BEGIN{RS="";OFS=","};$1=$1' It doesn't create a spurious comma at the end and properly terminates the output by a newline.


3

Using Awk's output record separator, ORS: awk 'BEGIN{ORS=",";}1' infile


4

find is an executable not an awk function. S, if you want to call an executable within awk, you have to do that with the system() function. cmd | awk '{system("find " $6 " -xdev -type f -perm -4000 -print")}'


1

try netstat -antulp |\ awk 'NR == 1 { skip } NR == 2 { printf "%s %-19s\t%s %-18s\t%s %s\n",$4,$5,$6,$7,$9,$10} NR > 2 { printf "%-24ss\t%-24ss\t%s\n",$4,$5,$6 ;}' where first line skipped second line, I pick field you need (and adjust column size) all other line, print field with adjustement I fold the line for a better view, this ...


2

From the awk manual: The awk utility shall interpret each input record as a sequence of fields where, by default, a field is a string of non-blank non-newline characters. This default blank and newline field delimiter can be changed by using the FS built-in variable or the −F sepstring option. You can change the behavious using the "-F" option ...


1

If you are really using gawk (though you may be using nawk, or mawk, in which case this won't apply), you can do this natively using one of the loadable extensions available since v4.0. I'm using gawk-4.1.x (v4.0 had a variation on the syntax for loading extensions). Loading the filefuncs extension adds (amongst others) a stat() function: @load "filefuncs" ...


1

If your shell supports it, you could use a here document to read the commands from standard input, with a quoted delimiter to prevent shell expansion of the awk variable $5 awk -f- somefile << "EOF" {sum+=$5} END { print "Average = ",sum/NR} EOF Or just put the commands in a file and run that with awk -f.


1

You don't need '' (strong quotes), you can use the weaker form "", except you then need to escape the "s. awk "{sum+=\$5} END { print \"Average = \",sum/NR}" But why?



Top 50 recent answers are included