Tag Info

New answers tagged

1

With sed you could accumulate non-empty lines in the Hold buffer, exchange on each empty line (and on last line) and if the pattern space matches remove the first char (\n) and print: sed '/^$/!{H;$ba;d};//ba;: a;x;/PATTERN/s/.//p;d' infile so in your case: sed '/^$/!{ # if line isn't empty H # append to hold space $ b ...


1

You could use GNU grep: grep -oP 'FREEBYTES.*percent="\K[0-9.]+' file Or sed: sed -n '/FREEBYTES/{s/.*percent="\([0-9.]*\).*/\1/p}' file Or GNU sed: sed -rn '/FREEBYTES/{s/.*percent="([0-9.]+).*/\1/p}' file64 Or awk: awk '/FREEBYTES/{sub(/.*percent="/,""); sub(/".*/,""); print}' file Or perl: perl -ne 'print if ...


0

any field can be pick up from an XML file, for instance with xyz.xsl having <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output type="text" /> <xsl:template match="//XYZ"> <xsl:value-of select="@percent" /> </xsl:template> </xsl:stylesheet> a mere ...


0

awk -F[=\"] '/<XYZ FREEBYTES/ {print $9}' This will find the line with <XYZ FREEBYTES match and print 9th filed with delimiter = and ".


5

nc writes its output to standard error, you need: nc -zvv localhost 31000-32000 2>&1 | grep succeeded The 2>&1 will redirect standard error to standard output so you can then pipe it to grep.


2

^=* means search for a line starting with zero or more equal signs. If you want lines starting with = just use ^=


1

Just use awk (using grep seems redundant to me, since awk already can match a regular expression): awk '$0~/\s*\#define\s*\[.*\]\s*.*/ {print $3}' *.h Going through the expression in more detail: $0 ~ /regexp/ # look for the regular expression in the record \s* # whitespace, any number of times \#define # literal ...


1

Use grep -o to print only the matching part of the line. Obviously take the \s part out again, because you don't want that part.


0

Here's a simple sample script for you to try. It will search for user's aliases from the common /etc/aliases -file. #!/bin/bash # # Arguments: # $1 = user name to be searched for # $2 = location for e-mail log file # # First, let's create a temporary working directory. WORKDIR=$(mktemp -d) cd ${WORKDIR} # # Create a file for user's user name and all ...


0

In this particular case - sorted input, so all con... words are listed before pro... words - you could use awk to store the lines matching ^con in an array and when you reach the lines matching ^pro, replace pro with con and print if the result is in array: awk '/^con/{arr[$0]=$0}; /^pro/{c=gensub(/pro/, "con", 1) if (c in arr) print c, $0}' ...


1

From my earlier comment to the question itself: egrep '^(pro|con).* /usr/share/dict/words | sed -nE 's/^(pro|con)(.*)/\2/p' | sort | uniq -d will give you a list of all the word-bases that have both a pro and con prefix: The initial egrep grabs all the words with pro and con prefixes. We then use sed to strip off pro and con from the beginning of each ...


0

This will print out the words without the pro|con prefix: grep '^\(pro\|con\)' /usr/share/dict/words | cut -c 4- | sort | uniq -c | awk '$1 == 2 {print $2}'


3

Without line breaks, grep is buffering all of the input, so that it can show you the "line" where the string appears. Two questions: Do you need the adjacent content? are there spaces or other characters separating the tokens? If you don't need the context of the adjacent content and there are spaces separating tokens, just use tr to turn spaces into ...


2

How about: tr '|' '\n' | sed -n 's/=Y$//p'


1

perl -nE 'say join(",",/(\w+)=Y/g)'


2

awk can read records based on a regex delimiter of your choice. eg '[|\n]' It can also split records into fields on the delimeter of your choice. eg. '=' The ternary operator (condition)?: prevents a leading comma. awk -F= -vRS='[|\n]' '$2=="Y"{ printf (i?",":"")"%s", $1; i=1 }' output: p2,p6 If a trailing newline is needed, it can be appended in ...


6

Using Perl compatible regular expression in grep: grep -Po '..(?==Y)' <file Result: p2 p6


1

a simple awk awk -F\| '{for (i=1 ; i<= NF; i++) if ( $i ~/Y/ ) { split($i,A,"=") ; printf "in %d : %s\n",i,A[1] ;}}' where -F\| use | as separator {for (i=1 ; i<= NF; i++) scan through pattern if ( $i ~/Y/ ) if found { split($i,A,"=") ; printf "in %d : %s\n",i,A[1] ;} split it and print it output in 3 : p2 in 11 : p6 use printf ...


3

Try this: echo 'p1=X||p2=Y||p3=X||p4=X||p5=X||p6=Y||p7=X' | grep -o '[^|]*=Y' | cut -d= -f1 | sed -e 'N;s/\n/,/g' Output: p2,p6


2

ps -fCsshd That's probably all you need on a linux system.


2

What you need is ps -e | grep sshd (ps -ef is listing processes and their arguments, ps -e lists only the processes).


5

The better way is to use pgrep for that task. It seems to be available on most systems these days.


-1

In the same way as Andrew, you can use [] : ps -ef |grep [s]shd It will be taken as a simple BRE regex, and will not match itself. I consider it less intrusive than the backslashes, for which you'll have to care about "what interpreter will take them into account first".


3

When available, just use pgrep which was designed to properly overcome this issue, eg: ps -f $(pgrep sshd) This method is cleaner than the hackish workarounds usually suggested.


1

Change the pattern so it is a regular expression that will not match the literal text. ps -ef | grep [s]shd


-1

You can use grep itself to avoid that result: ps -ef|grep your_search_chain|grep -v grep


3

There are a number of ways, more or less hacky and awkward. My favourite is to insert a double-backslash before a letter in my search string: ps -ef | grep ss\\hd The \\ is interpreted to \ by the shell, and \h is interpreted as equivalent to 'h' by grep. So it is effectively searching for 'sshd' as before. However, its own command string no longer ...


1

(hopefully) the final product find . -name \*.gz -type f -exec gzcat {} + | sed -ne'/^ *ID:/h;/No Profile/!d;x' \ -e's/^ *ID:\([^ ]*\).*/\1/p' So that will recursively find all of the regular files rooted in the current directory with filenames which match the pattern *.gz and call zcat as few times as is necessary to iteratively uncompress every ...


1

Figured it out. It does not like the -O grep -rlZ "wrongtext" ~/Library/Calendars | xargs sed -i '' 's/wrongtext/righttext/g'


2

That should do it: grep -rlZ "wrongtext" ~/Library/Calendars | xargs -0 sed -i '' 's/wrongtext/righttext/g' I added the -Z parameter to grep to add a zero byte instead of a newline after every filename. So the command works also with strange filenames. xargs then reads the input delimited by the zero byte with -0 and calls the sed command.


2

Edited New approach: removing line breaks. Assuming that you have only one ID per gzipped file, you can try the following: gunzip -c file.gz | sed -e ':a;N;$!ba;s/\n/ /g' -e '/^[[:space:]]/d' -e 's/^ID:\([[:alnum:]]*\).*Warning - No Profile Data.*/\1/' -e '/^ID:/d' gunzip -c extracts the file to stdout sed collapses all lines into one, then removes all ...


2

You could work with awk instead: zcat file.gz | awk '/No Profile Data/{printf "%s\n%s\n%s\n", b, a, $0} {b=a;a=$0}' zcat prints the content of the gzipped file to the standard output awk then searches for the string "No Profile Data" and prints the two previous lines


4

Your question is about find binary files that contain a pattern (and we have already very good answers!). Complementary we may like to get the occurrences. I often use grep -aPo '.{0,20}pattern.{0,20}' binfile to get a surrounding context of 20-char.


10

The command strings will extract all ascii data from a file, if you then grep its output, you can search for your data: strings <filename> | grep "search text"


9

With GNU grep, you can use -a option to make it treats binary files as text files: grep -alir string -- file If your grep version does not support -a, you can use ack instead. With ack 1.x, you need to include -a option, with ack 2.x, you don't, since when searching include non-text file by default (only ignored non-text file when you did not specify any ...


1

Git grep is not using the GREP_COLORS environment variable. Instead you should add custom entries in you ~/.gitconfig For example: [color "grep"] linenumber = yellow bold match = red filename = magenta


0

Use the file command on Linux/Ubuntu. If the file is in DOS format, the output will include the words, "with CRLF line terminators". If the file is in UNIX format, no such words will be in the output. In the example, below, del.txt is in DOS format and del is in UNIX format. $ file del.txt del.txt: C source, ASCII text, with CRLF line terminators $ echo ...


0

Pure bash: shopt -s globstar for i in **/*.c;do read -N 1 h < "$i"; if [[ $h != "<" ]]; then # echo "found $i"; # do stuff with "$i" fi; done read -N 1 reads a single character from the file, without having to fork/exec anything. If you just need a list of filenames, use something else that makes it easier to use the ...


3

If your awk supports the nextfile statement (most do): find . -name '*.xml' -type f \( -size 0 -print -o -exec awk ' !/^</ {print FILENAME}; {nextfile}' {} + \)


4

You have some solid answers already, however I'll offer an alternative - the XML spec is quite strict, and files that don't start with < isn't actually XML at all. So a simple approach might be to simply test if the file is 'valid' or not. All XML parsers can do this, but here's an example: #!/usr/bin/perl use strict; use warnings; use XML::Twig; ...


1

find .//. -name \*.xml -type f -exec head -n1 {} + | sed -ne:n -e'\|^==> \.//\.|!{H;$!d' -e\} \ -ex -e'\|\.xml <==\n|!{G;x;d' -e\} \ -e's|[^/]*//\(.*\) <==\n[^<]*$|\1|p' head lists filenames all on its own. So you can just -exec it and have sed watch its input for head's report on the names for those files which don't match a ...


3

To grep the first line of each file and print if they match you can use xargs and awk find . -type f -name "*.xml" -print0 | xargs -0 -I{} awk 'NR==1&&!/^</' {} To print the filename of the files find . -type f -name "*.xml" -print0 | xargs -0 -I{} awk 'NR==1&&!/^</{print FILENAME}' {}


3

grep -F is POSIX. fgrep is not. Use grep -F and make it clear that your script depends on a POSIX grep. Clearly stated dependencies are all you need. I guess you could use both... if command -v fgrep then _grepf(){ fgrep "$@"; } else _grepf(){ grep -F "$@"; } fi >/dev/null ...but doing stuff like that is hacky and usually a waste of time. Set ...


2

sed -ne:n -e's/\n/&/3;tp' \ -e'$!{N;bn' -e\} \ -e:p -e'/\n.*N.*\n/p' \ <in >out If your input occurs in regular blocks of four lines per, and if you're looking for at least one N that doesn't occur on the last of these four, then the above bit of sed should suit your needs. It does assume that your entire input file will be ...


2

join -t, -1 2 -2 1 <(nl -s, -ba -nrz file1 | sort -t, -k2) \ <(sort -t, -k1 file2) | sort -t, -k2 | cut -d, -f1,3- The lines in file1 are numbered and the result is sorted on 2nd field. This is then joined with file2 (sorted on 1st field): air,000001,4,21,01,13,3,2 cat,000006,1,5,6,3,1 dude,000004,1,31,42,1 hand,000003,1,4,2,1,6 ...


2

This should do it: awk -F, 'FNR==NR {a[$1]; next}; $1 in a' file1 file2 edit: Interpreted the wrong file for the ordering. A new attempt (requires gawk if that is acceptable) gawk -F, ' FNR==NR {a[NR]=$1; next}; {b[$1]=$0} END{for (i in a) if (a[i] in b) print b[a[i]]} ' file1 file2 edit 2: With nowmal awk, and swapping the files around: ...


-1

Perl to the rescue! Save as remove-blocks.pl, run as perl remove-blocks.pl input_file > output_file. #!/usr/bin/perl use strict; use warnings; my @four_lines; # Buffer to hold a block. while (<>) { # Read the input line by line. if (@four_lines < 3) { # Not reading the last line? ...


0

I'm late to the party here but everyone missed this "I host about 200 websites.." Your question indicates you are looking for error_log files which I take to mean the php error logs. Your best bet is to use find and depending on your system and where the web root directories are located it should look something like this find . -type f -name "error_log" ...


0

You could match the first part of the pattern grep "COST -> 1" hugedatafile.txt but this would also match entries of COST -> 143 So we need to fancy it up a bit grep -e "[1-9]\.|[1-9][0-9]\.[0-9]|100" hugedatafile.txt The "|" is an 'or' switch for extended grep. There are 3 conditions being tested here. Any number between 1.0 and 9.(whatever decimal) ...


0

in the line awk '/master/{$4="$1";$5="$2"}' a.txt >newa.txt "$1" doesn't hold value passed from shell. you should write awk '/master/{$4="'$1'";$5="'$2'"}' a.txt > newa.txt so that $1 frm calling shell is put in awk file.



Top 50 recent answers are included