Getting a line number of a specific word from a text file [duplicate] - bash

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

Related

Need help inserting text AFTER two consecutive matches using sed on macOS [closed]

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.
Improve this question
How do I insert text after two consecutive matches in a file using sed. Is this possible? If it is, how do I do the following?
My file:
abcdef()K
.xyz 1
('I want to add text here')
Note that the .xyz will be same but it can be 1 or 2 or 3 or any other number. But .xyz WILL DEFINIETLY BE under abcdef()K. I want to add text exactly AFTER the .xyz part.
Please do help guys. Thanks.
You can use this perl:
perl -0777 -pE 's/^(abcdef\(\)K\R\.xyz.*\R?)/\1(your text here)\n/mg' file
Or this awk:
awk -v ins="(your text)" '/abcdef()K/{f=1; print; next}
f && /\.xyz.*/{f=0; print; print ins; next}
{f=0; print}' file
If .xyz [0-9] is constant and is always on the line below the first match, then you can append the new line with sed
$ sed '/\.xyz [0-9]*/a New Text' input_file
abcdef()K
.xyz 1
New Text
Otherwise, you can match the first line, add a condition to substitute the match on the second line if the first matched, substitute it for itself, jump to new line and append the new text.
sed '/a.*()K/ {N;s/\.xyz [0-9]*/&\nNew Text/}' input_file
abcdef()K
.xyz 1
New Text
This might work for you (GNU sed):
sed -e '/abcdef()K/!b' -e ':a;n;/\.xyz/!ba' -e 'a new text added' file
If a line does not contain abcdef()K bail out.
Print the current line and fetch the next.
If the current line does not contain .xyz, repeat above.
Otherwise, append new text added to the current line.
sed -e '/abcdef()K/!b' -e ':a' -e 'n' -e '/\.xyz/!ba' -e 'a new text added' file
Or:
cat <<\! | sed -f - file
/abcdef()K/!b
:a
n
/\.xyz/!ba
a new text added
!

How can I delete empty line from my ouput by grep? [duplicate]

This question already has answers here:
Remove empty lines in a text file via grep
(11 answers)
Closed 4 years ago.
Exists way to remove empty lines with cat myfile | grep -w #something ?
I looking for simple way for remove empty lines from my output like in the way the presented above.
This really belongs on the codegolfing stackexchange because it's not related to how anyone would ever write a script. However, you can do it like this:
cat myfile | grep -w '.*..*'
It's equivalent to the more canonical grep ., but adds explicit .*s on either side so that it will always match the complete line, thereby satisfying the word boundary conditions imposed by -w
You can pipe your output to awk to easily remove empty lines
cat myfile | grep -w #something | awk NF
EDIT: so... you just want cat myfile | awk NF?
if you have to use grep, you can do grep myfile -v '^[[:blank:]]*$'

How to number output in list in bash? [closed]

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 4 years ago.
Improve this question
I want to number each line that gets outputted when i list a directory, so that instead of typing out the full name of the object, i can identify it with a number in the list. In Bash.
Ex. os-list is a directory I use to store numerous of objects that are ever changing.
os1.xxx.iso is the object name.
From
ls os-list
os1-xxx.iso
os2-xxx.iso
What is the path?: os1-xxx.iso
To
ls os-list
[1]os1-xxx.iso
[2]os2-xxx.iso
What is the path? 1
What is the term that im looking for this kind of operation in bash?
The command select can be used:
files=$(ls os-list)
select choice in ${files[#]}; do
break
done
echo "${choice}"
You can modify this to your needs, just look for more examples with select.
I would change the prompt (PS3="What is the path: ") and replace the break in the select loop (check for a valid response).
I want to number each line that gets outputted when i list a directory
For your exact desired output format:
ls | nl | sed 's/^[ \t]*//' | sed -r 's/^[0-9]*()/[\0]/' | sed 's/\t//'
or
ls | cat -n | sed 's/^[ \t]*//' | sed -r 's/^[0-9]*()/[\0]/' | sed 's/\t//'
If you only want to have number as reference, and not in your exact desired format then simply:
ls | cat -n
or
ls | nl
would suffice, sed pipe is added to enclose given number in square brackets and remove starting/trailing spaces to conform to your desired output. Admitedly, this could be done with awk as well, pipe is not optimized, just given as reference point.
Edit:
with awk like so:
ls | cat -n | awk '{print "[" NR "]"$2}'
Selecting filename based on index (example with index 12 given):
ls | cat -n | awk '{print "[" NR "]"$2}' | grep "^\[12\]" | sed 's/^\[12\]//'
Note of caution: this approach supposes that between listing and selecting no file is added (if file is added in between and your sort order is messed up 12th file in listing and 12th file in select might ended not being the same file).

How to crop a word [closed]

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 7 years ago.
Improve this question
I have this list of inputs :
imalex
thislara
heiscarl
how to get :
alex
lara
carl
grep
Use grep to take the last four chars:
grep -o '.\{4\}$' file
The -o option makes sure only matched parts are printed.
sed
Using sed we can achieve a similar result:
sed 's/.*\(.\{4\}\)$/\1/' a
Here we capture the last four digits and replace each line with those last four digits. They are captured in a group \( \) and inserted \1.
read & tail
We can also grab the last five chars (including the newline) of each line using tail and a -c option. We do that for each line using read.
while read line; do
tail -c 5 <<< $line
done < file
2 answers using substring arithmetic
bash:
while read word; do
echo "${word:${#word}-4}"
done <<<"$list"
awk
echo "$list" | awk '{print substr($NF, length($NF)-4+1)}'

Omit lines from the beginning or end of a file in Bash [duplicate]

This question already has answers here:
Remove the last line from a file in Bash
(16 answers)
Closed 9 years ago.
Given a text file a.txt, how to cut the head or tail from the file?
For example, remove the first 10 lines or the last 10 lines.
to list all but the last 10 lines of a file:
head -n -10 file
to list all but the first 10 lines of a file:
tail -n +10 file
To omit lines at the beginning of a file, you can just use tail. For example, given a file a.txt:
$ cat > a.txt
one
two
three
four
five
^D
...you can start at the third line, omitting the first two, by passing a number prepended with + for the -n argument:
$ tail -n +3 a.txt
three
four
five
(Or just tail +3 a.txt for short.)
To omit lines at the end of the file you can do the same with head, but only if you have the GNU coreutils version (the BSD version that ships with Mac OS X, for example, won't work). To omit the last two lines of the file, pass a negative number for the -n argument:
$ head -n -2 a.txt
one
two
three
If the GNU version of head isn't available on your system (and you're unable to install it) you'll have to resort to other methods, like those given by #ruifeng.
to cut the the first 10 lines, you can use any one of these
awk 'NR>10' file
sed '1,10d' file
sed -n '11,$p' file
To cut the last 10 lines, you can use
tac file | sed '1,10d' | tac
or use head
head -n -10 file
cat a.txt | sed '1,10d' | sed -n -e :a -e '1, 10!{P;N;D;};N;ba'
IFS=$'\n';array=( $(cat file) )
for((i=0;i<=${#array[#]}-10;i++)) ; do echo "${array[i]}"; done

Resources