Tag Info

New answers tagged

1

You recorded the output of the command to a terminal, so it decided to use terminal features. Specifically, apt-get uses carriage return characters to move the cursor back to the beginning of the current line and overwrite the text that was on the line. To emulate the effect of carriage returns, remove all text before one: <transcript sed 's/.*\r//' ...


1

(head;tail) doesn't work in a pipeline if the tail is part of the first block consumed by head: $ seq 9|(head -n2;tail -n2) 1 2 $ seq 9000|(head -n2;tail -n2) 1 2 8999 9000 You can use gsed -u 2q as an unbuffered alternative to head -n2: $ seq 9|(gsed -u 2q;tail -n2) 1 2 8 9


0

In this answer, you need to specify how many fields make up the "key". Apparently in your real data it's 32, but in your sample data, the first 7 fields is the key: awk -F'|' -v nKeys=7 ' NR==FNR { suff = "" for (i=nKeys+2; i<=NF; i++) suff = suff FS $i NF = nKeys suffixes[$0]=suff next } { ...


1

First, refactoring your question: How can I construct a perl script that inputs data from snmpwalk for each IP/Host and outputs a table for each OID. Second, your example snmpwalk commands make no sense. It might make sense if OID were a variable. You problably mean to use snmpwalk -v2 -c public ${ip} ${OID} You could do something like this: ...


0

If I understand your demands correct this does what you expect: awk '/ddddddddddddddd/,/hhhhhhhhhhhhhhh/ { print > "excerpt" } 1' infile | tail -f awk works as filter on file "infile"; it prints everything in between the given patterns into a file "excerpt", and it also prints every line to standard output, which is then processed by tail -f as usual. ...


0

Maybe you can use awk's range patterns here: tail -f logfile | awk '/ddddddddddddddd/,/hhhhhhhhhhhhhhh/' If you need to circumvent the SIGPIPE problem you can use socat instead of tail for the job: socat -u file:logfile,ignoreeof "system:'stdbuf -o0 awk /ddddddddddddddd/,/hhhhhhhhhhhhhhh/'" > logfile.new


3

This is exactly what retail does. retail is tail with regular expressions, a tool I wrote for exactly the use case you have here. In your case, you'd use: retail -f -r ddddddddddddddd -u hhhhhhhhhhhhhhh logfile > log.tmp -f is the standard tail -f option. -r takes a regular expression to use to start the range of lines to include, and -u takes a ...


1

Try to use pdsh. A lot of examples available on Project Page If you like to use simple bash script: #!/bin/bash HOSTS="host1 host2 host3" USER=root CMD="ls" for host in $HOST; do ssh ${USER}:{$host} "$CMD" done In all cases you would need to tune no-password auth using keys and append to ssh command: -i /path/to/key Example to use: #!/bin/bash ...


0

I think this is better.. less -N +F <filepath>


0

Awk solution awk ' /Routeur/{ i=0; host[$1" "$2]=$4; k++; param[k]=$5; next } { i++ Data[k,i]=$0 } END{ printf("%-12s%-15s","routerName","IPadd"); for(j in param) printf("%-17s",param[j]); print "\n"; for(h in host) for(i=1;i<3;i++){ printf("%-12s%-15s",h,host[h]); for(j ...


1

Perl solution, using Text::Table for nice output formatting: #!/usr/bin/perl use warnings; use strict; use Text::Table; my %t; my ($router, $ip, $column); while (<>) { if (/==>/) { ($router, $ip, $column) = /(.*) ==> ([0-9.]+) (\S+)/; } else { chomp; push @{ $t{$router}{$ip}{$column} }, $_; } } my @columns ...


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


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


0

Opens both compressed and non-compressed, in chronological order. ls syslog* | tac | xargs zcat -f | less


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


3

A straightforward awk solution (without pre-sorting or any external tools) could be: awk ' FNR==1 { next } NR==FNR { a[$1] = $2 ; next } a[$1] != $2 { print $1, ":", $2, "does not match", a[$1] } ' file1 file2 Output for your data: 6 : strawberry does not match papaya 3 : orange does not match mango 1 : mango does not match apple 2 : papaya does ...


3

If you always have the same number of entries in each files, and if each ID can be join with the same ID in the other file, you could do something like this (this is a proof of concept, you have to improve it): $ join -j1 <(sort -n file1.txt) <(sort -n file2.txt) | awk '{if($2!=$3){k="==> BUG"}else {k=" ==> OK"}print $0,k}' Id Value Value ...


0

Since Mac OS X doesn't have the -h option for sort, so I tried and learned sed and awk for a first attempt: du -sk * | sort -g | awk '{ numBytes = $1 * 1024; numUnits = split("B K M G T P", unit); num = numBytes; iUnit = 0; while(num >= 1024 && iUnit + 1 < numUnits) { num = num / 1024; iUnit++; } $1 = sprintf( ((num == 0) ? "%6d%s " : "%6.1f%s ...


3

The redirection to file.txt at the end of your paste command is truncating your file before paste has a chance to read it. Try echo 2 | paste file.txt - > file2.txt or if you have sponge installed echo 2 | paste file.txt - | sponge file.txt


3

Here's another way to do it. Let's say we have two files, file1: Dave 734.838.9800 Bob 313.123.4567 Carol 248.344.5576 Mary 313.449.1390 Ted 248.496.2204 Alice 616.556.4458 Jimmy 324.555.8867 Harry 422.858.2354 Lou 788.907.6859 and file2: Bob Tuesday Carol Monday Jimmy Wednesday Ted Sunday Alice Wednesday Dave Thursday Harry Monday Mary Saturday Lou ...


2

You can use the sponge function, that first soaks the stdin and then writes it to a file, so: sort < f | sponge f The downside of sponge is that it will store the output temporary in memory, which can be problematic for large files. Otherwise you have to write it to a file first and then overwrite the original file. As however is pointed out by other ...


2

awk ' BEGIN { print "Name On-Call Phone" split("MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY", days); } NR == FNR { day[$1] = $2; next } { lines[toupper(day[$1])] = $1 OFS toupper(day[$1]) OFS $2 } END { for (i=1; i<=7; i++) { if (lines[days[i]]) print lines[days[i]] ...


1

Use -o or try the vim-way: $ ex -s +'%!sort' -cxa file.txt


3

The join utility is intended for exactly this kind of problem: it joins two files based on one of their fields, by default the first one. The files should be sorted first; so join <(sort file2) <(sort file1) | column -t produces Alice Wednesday 616.556.4458 Bob Tuesday 313.123.4567 Carol Monday 248.344.5576 Dave Thursday ...


2

Based on the input you show in your question, this should work: $ grep -oP '^[ @]*R.* \K.*' gitolite-info-output SecureBrowse anu-wsd entrans git-notes gitolite gitolite-admin indic_web_input proxy testing vic This is using GNU grep's -P switch to enable Perl Compatible Regular Expressions which give us \K : "Exclude anything matched up to this point". ...


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


2

Firstly, this is a UUOC (useless use of cat). There is no good eason to use cat here, sed is perfectly able to read files itself, and even if it wasn't, then redirecting standard input from the file would be equivalent to piping it. esc=$(echo -e '\e') sed "s,\(.*\) \(.*\) \(.*\) \(.*\),$esc[31m\1 $esc[34m\2 $esc[33m\3 $esc[32m\4$esc[m," file4 This ...


0

As @Kevin suggested, you can use wc command to count lines in a file. However, wc -l test.txt will include the file name in the result. You can use: wc -l < test.txt to just get the number of lines without file name in it. Give it a try.


1

There are many ways to do this.  Here’s one using only the shell (no external programs): #!/bin/sh IFS=, read -r f1 f2 printf "%s,%s-%s" "$f1" "$f2" "$f2" while IFS=, read -r f1 f2 do printf ",%s-%s" "$f2" "$f2" done printf "\n" Put the above into a file called, say, myscript.sh, do chmod +x myscript.sh, and then run ./myscript.sh with input and ...


1

Starting with this file: $ cat file uk,1234560000 uk,6789067000 uk,4567890000 If the formatting in the question is correct, I believe that this does what you ask: $ awk -F, '1==NR{printf "%s",$1} {printf ",%s-%s",$2,$2} END{print""}' file uk,1234560000-1234560000,6789067000-6789067000,4567890000-4567890000 How it works awk will read in a record (line) ...


1

The problem is the way tou get the result of your command and put it in your variable $rotation: You should replace rotation="xrandr -q --verbose|grep LVDS1|cut -b37-42" with rotation=$(xrandr -q --verbose|grep LVDS1|cut -b37-42) UPDATE: The explication is that the differents quotes expand commands and variables in different ways: simple quotes ' ...


2

Here is an awk way to achieve what you want: awk ' NR==FNR { k[$1] ; next } { for (i=1; i<=NF; i++) if($i in k) $i="" ; gsub(/ +/," ") } 1 ' champs.txt t.txt (The gsub is just for convenience, it compresses sequences of blanks.) For you sample data the result is: * student name age professors departement DPTNUM= 20


0

user@host:/usr$ alias duh="du -s -B1 * | sort -g | numfmt --to=iec-i --format='%10f'" user@host:/usr$ duh Gives: 4.0Ki games 3.9Mi local 18Mi include 20Mi sbin 145Mi bin 215Mi share 325Mi src 538Mi lib Unfortunately I can't figure out how to get two decimals accuracy. Tested on Ubuntu 14.04.


0

Through python. $ python -c "import sys; with open(sys.argv[2]) as f: for line in f: if sys.argv[1] == line.split(':')[0]: print ' '.join(line.strip().split(':'))" def file def 423324 arbitrary value string Just run the above script on terminal with the search keyword as firs argument and the file-name as second argument.


3

Here is a sed alternative: sed -n '/^def:/s/:/ /gp' myfile.csv If you pass the string as first positional parameter: sed -n "/^$1:/s/:/ /gp" myfile.csv


3

My apologies, but reading your statement The exact text I want to insert after end of the HEAD section is... led me initially to believe we were talking about appending text to file section rather than inserting before - so I wrote that answer first. After rereading the question, and more closely studying your example sed command - I think I understand this ...


4

In-place sed requires making a backup file during the process. The -i option on Apple's sed requires an extension argument (for the backup file it creates) and consumes the next argument. That means you're telling it you want it to make a backup file with the extension "#s</head>#...". The error means it thinks you're referring to the append command. ...


8

Your awk syntax is a little wrong. #!/bin/bash awk -F: -v keyword="$1" '$1 == keyword {$1=$1; print}' myfile.csv The trick here is reassigning the value of one of the fields forces awk to recalculate $0 using the output file separator. Here, the default OFS is a space, so assigning the value of $1 to itself changes the colons to spaces. A non-awk way to ...


2

You should use sed, in my opinion. In the first place, it will usually outperform awk, and, in this case, it offers a more simple solution overall: sed -n 'N;s/Node \([^ ]*\).*Logical/\1/p' <infile ...which, given your example data, prints... WWN:2000000c50f1c8da Path:/dev/rdsk/c1t1d0s2 WWN:2000000c50f1d9c9 Path:/dev/rdsk/c1t0d0s2 For each line ...


2

The short answer which isn't mentioned yet in awk awk '/Node/{n=$2; getline; print n " "$2; next}' filename /Node/ # Look for pattern Node and store the second field in n variable n=$2 getline # Move to the next line (This is where Logical line starts), print what's stored in n variable and then second field of the next line. next # Stop processing ...


3

The reason it's faililng is because you are only printing the variables you capture once, in an END{} block. This means that only the last two will be printed. @Costas already gave you a more elegant approach, but you could also use the same logic you were trying with a small change: awk '{ if(/Node/){ if(length(n)){print n,l} ## if we have an n, ...


1

If you want awk awk -v RS=' Node ' -v FS=' Logical Path:| ' '/WWN/{print $1,$6}' Other (may be simple) awk -F":| " '/Node/{printf "%s ", $3":"$4}/Logical Path/{print $4}' with remain Path: awk '/Node/{printf "%s ", $2}/Logical Path/{print $2}' or awk '/Node/{wwn=$2}/Logical Path/{print wwn,$2}' And GNU sed at last: sed -n '/WWN/{s/^\s*\S* *\|\( ...


0

Tip: Use database tools for database work. If you end up spending all of your time just working out the mechanics of looking things up as opposed to doing the lookups themselves, and your commands to look things up are long combinations of perl, sed, awk, and grep that start to resemble modem line noise, then it is time to consider using actual database ...


0

#!/bin/bash while read line do grep -A 1 $line filea >> filec done < fileb



Top 50 recent answers are included