Grep last N characters of each line in a file [closed] - unix

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a file with millions of lines.And each line ends with a format say "XXX:some value" .. I want to grep only this word "XXX:some value" from each line and put it in a separate file.How can i achieve this using grep command?
PS: "some value" can be any string

To get last n characters of each line using awk:
cat file
asdf
asdfg
asdfgh
awk -vn=2 '{print substr($0,length($0)-n+1)}'
df
fg
gh
Or do you like to get data after XXX, then do:
echo "here is my line XXX:22" | awk -F"XXX:" '{print $2}'
22

Use the -o flag:
grep -o 'XXX:.*' <input >output

You could just use grep as below:
grep -ow 'XXX:some value' myfile.txt > patternMatched.txt

Related

how to find the number of occurence of a particular word from text file in unix [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a text file in which i have 17000 lines, example:
another instance started
another instance started
instance not started
bss instance started
like this.
I have to find the number of times the word 'another' is in printed in the above example my output should be 2. I need shell script command for the above example. Can any one help me with shell coding or command?
First you should try to search harder, the answer can be found easily.
Second:
grep -c another yourfilepath
Just use good old grep.
grep -c 'word' file
\<another\> is a word boundary and it won't match abcanother or anotherxyz etc.
grep -o '\<another\>' file.txt | wc -l
grep -c flip article.txt
10
See http://www.linuxjournal.com/article/2384
and http://www.cyberciti.biz/faq/grep-regular-expressions/

Unix command to cut file and recreate new one [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
I have a input.txt which contains data like this
123
1234
1223
I want it to convert to another file output.txt and file should look like this
'123','1234','1223'
Can you someone please let me how it can be done in unix?
You can try this,
tr -s '\n' < input.txt | sed "s/.*/'&'/g" | tr '\n' ',' | sed 's/,$//g' > output.txt
I'm afraid I can't you with bash. Try this in Python:
InputFilepath = /path/to/input.txt
OutputFilepath = /path/to/output.txt
with open(InputFilepath, "r") as f:
words = f.read().splitlines() #may be not needed?
result = ','.join(f)
with open(OutputFilepath, "w") as g:
g.write(result)
I bet there is a cleaner way to do this but can't think of it so far.
# 1 2 3 4
sed "/^[ \t]*$/d; s/\(.*\)/'\1'/" input.txt | tr "\n" "," | sed 's/,$//'
remove blanks lines (including lines containing spaces/tabs).
add single quotes around each line
replace new-line with comma
remove trailing ,
You could use sed
cat input.txt | sed -n "s!\(.*\)!'\1'!;H;\$!b;x;s!^\n!!;s!\n!,!g;p"
Read each line in (not printing by default -p), and then append it to the hold space H - then stop for all lines except the last one \$b.
On the last line - copy the hold space into the pattern space x, ditch the first newline (the hold space has a newline in it to start with), and then replace the remaining newlines with ','. Finally print out the pattern space p.
You could use a perl script
#!/usr/bin/perl
my #lines = <>;
chomp(#lines);
print join(',', map { "\"$_\"" } #lines), "\n";
./script input.txt
Here is an awk version
awk 'NF{s=s q$0q","} END {sub(/,$/,x,s);print s}' q="'" file
'123','1234','1223'
How it works:
awk '
NF { # When line is not blank, do:
s=s q$0q","} # Chain together all data with ' before and ',
END { # End block
sub(/,$/,x,s) # Remove last ,
print s} # Print the result
' q="'" file # Helps awk to handle single quote in print, and read the file
With GNU awk for a multi-char RS:
$ awk -v RS='\n+$' -v FS='\n+' -v OFS="','" -v q="'" '{$1=$1; print q $0 q }' file
'123','1234','1223'
It just reads the whole file as one record (RS='\n+$') using sequences of contiguous newlines as the input field separator (FS='\n+') then recompiles the record using ',' as the output field separator (OFS="','") by assigning a field to itself ($1=$1), and prints the result with a ' at the front and back.

tail multiple files and grep the output [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I would like to grep a pattern from multiple log files which are being constantly updated by some processes and tail the output of this grep continuosly.
Below command doesnt work and I get
tail: warning: following standard input indefinitely is ineffective
tail -f | grep --line-buffered "Search this: " /var/links/proc2/id/myprocess*/Daily/myprocess*.log
Can someone help sort this out?
You should have a look at multitail tool (Install using sudo apt-get install multitail)
In short, with multitail, you need to use the --mergeall flag for viewing output of all in one place
multitail --mergeall /var/links/proc2/id/myprocess*/Daily/myprocess*.log | grep --line-buffered "Search this: "
You can do the same without using grep
multitail -E "Search this: " --mergeall /var/links/proc2/id/myprocess*/Daily/myprocess*.log
To view the output individually using multitail, this will give the filename as well.
multitail -E "Search this: " /var/links/proc2/id/myprocess*/Daily/myprocess*.log
the mistake is that you give the files to the grep command and not the tail.
the tail -f needs to get the files as input. try:
tail -f /var/links/proc2/id/myprocess*/Daily/myprocess*.log | grep --line-buffered "Search this: "
to get also the file names (however it will not be like grep output it is):
tail /var/links/proc2/id/myprocess*/Daily/myprocess*.log | grep --line-buffered -e'^==> .* <==$' -e'Search this: '
This is an interesting question and the simple answer should be: Use the prefix switch with tail, but unfortunately this is currently not implemented in most versions of tail.
As I see it, you have two options: adapt the standard tools to the task (see Udys answer) or write your own tool with your favorite scripting/programming language.
Below is one way you could do it with the File::Tail::Multi module for perl. Note that you may need to install the module from CPAN (cpan -i File::Tail::Multi).
Save the following script e.g. mtail to your executable path and make the script executable.
#!/usr/bin/env perl
use File::Tail::Multi;
$| = 1; # Enable autoflush
$tail = File::Tail::Multi->new(RemoveDuplicate => 0,
OutputPrefix => 'f',
Files => \#ARGV);
while(1) { $tail->read; $tail->print; sleep 2 }
Change OutputPrefix to 'p' if you prefer full path prefixes.
Run it like this:
mtail /var/links/proc2/id/myprocess*/Daily/myprocess*.log | grep --line-buffered "Search this: "
You do not need to specify --line-buffered when grep is the last command, so this is sufficient:
mtail /var/links/proc2/id/myprocess*/Daily/myprocess*.log | grep "Search this: "

Remove character from a File [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Improve this question
Using UNIX Scripting it is possible to remove all the firsts characters from a file till a specific character is found ?
I have a file with "garbage" at the beginning. I want to remove that "garbage, meaning that all the character till the first "{" must be removed. How can I do this ?
cat file.txt | grep -A 1000000000 '{' | sed '1 s/^[^{]*//'
This will print the changed contents (i. e. without the garbage) to stdout. You can redirect this using > outfile.txt appended to the command:
cat file.txt | grep -A 1000000000 '{' | sed '1 s/^[^{]*//' > outfile.txt
And if you want to change the file in-place, this can be done by renaming the outfile.txt to the original name file.txt afterwards:
mv outfile.txt file.txt

Find the line number where a specific word appears with "grep" [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Is grep capable of providing the line number on which the specified word appears?
Also, is possible to use grep to search for a word starting from some certain line downward?
Use grep -n to get the line number of a match.
I don't think there's a way to get grep to start on a certain line number. For that, use sed. For example, to start at line 10 and print the line number and line for matching lines, use:
sed -n '10,$ { /regex/ { =; p; } }' file
To get only the line numbers, you could use
grep -n 'regex' | sed 's/^\([0-9]\+\):.*$/\1/'
Or you could simply use sed:
sed -n '/regex/=' file
Combining the two sed commands, you get:
sed -n '10,$ { /regex/= }' file
You can call tail +[line number] [file] and pipe it to grep -n which shows the line number:
tail +[line number] [file] | grep -n /regex/
The only problem with this method is the line numbers reported by grep -n will be [line number] - 1 less than the actual line number in [file].
Or You can use
grep -n . file1 |tail -LineNumberToStartWith|grep regEx
This will take care of numbering the lines in the file
grep -n . file1
This will print the last-LineNumberToStartWith
tail -LineNumberToStartWith
And finally it will grep your desired lines(which will include line number as in orignal file)
grep regEX

Resources