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
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have a file which contains below information-
SOURCE "defrust"
DESIGN "2_3"
SYSTEM SPICE
NETLIST SOURCE
NETS "NONE"
//NETS "NONE"
//VARIABLE "cell2"
DESIGN "hello one"
How can i use unix commands to delete full lines which are starting from SOURCE ,DESIGN and //
output be somewhat like-
SYSTEM SPICE
NETLIST SOURCE
NETS "NONE"
thanks in advance
Use this Perl one-liner:
perl -i.bak -ne 'print if !m{^(SOURCE|DESIGN|//)}' file_name
The Perl one-liner uses these command line flags:
-e : Tells Perl to look for code in-line, instead of in a file.
-n : Loop over the input one line at a time, assigning it to $_ by default.
-i.bak : Edit input files in-place (overwrite the input file). Before overwriting, save a backup copy of the original file by appending to its name the extension .bak.
^ : beginning of the line in regex.
SEE ALSO:
perldoc perlrun: how to execute the Perl interpreter: command line switches
perldoc perlre: Perl regular expressions (regexes)
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
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/
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 am trying to grep a list of IDs present in file1 from file2
I write:
grep -f file1 file2
The command gets stuck as if perpetually in the run phase.
Then I try:
fgrep -f file1 file2
This works in a flash.
The man page of grep says that fgrep is same as "grep -f". But then how come I get no output for "grep -f"
You cite the man page incorrectly! What is written there is this:
fgrep is the same as grep -F
Note the uppercase -F which is quite different to -f!
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 2 years ago.
Improve this question
Can some one please tell me how can I find the following.
List from /etc/passwd the UID and the user having the highest UID.
cat /etc/passwd | awk -F: '{print $3,$1}' | sort -n | tail -n 1
Instead of reading /etc/passwd, it would be better to get the output from
getent passwd
As you could be using another source of UIDs via nsswitch, such as LDAP.
/etc/passwd contains user information separated by colons. The user id is in the third column.
The sort command line tool can be used to sort the lines of a file. It has options, to choose which separator the columns are separated by, which column to sort by and whether to sort numerically or alphabetically.
So you can use sort to sort /etc/passwd by user id and then use tail to get the last line from that, which will contain the user with highest id.
getent passwd | awk -F : '$3>h{h=$3;u=$1}END{print h " " u}'
The getent output needs to be sorted for the awk command.
In addition, I found that nfsnobody (on Linux) can be ignored and the next highest UID is what is often needed. So this worked well:
getent passwd |sort -t: -k3 -n |awk -F: '$3>h{ph=h;pu=u;h=$3;u=$1}END{print h,u"\n"ph,pu}'
65534 nfsnobody 1002 user2