7
votes
Finding the line number of first occurrence of a text in bash script
With POSIX sed, you suppress normal output with the -n option, then for the line starting with c (pattern ^c), print the line number with = and quit:
sed -n '/^c/{=;q;}'
With GNU sed, you can use the ...
6
votes
Finding the line number of first occurrence of a text in bash script
Several solutions exist
with AWK
awk '/^c/ { print NR; exit}' "${varFileLog}"
/^c/: matches the line starting with c
print NR: prints the record (line) number
exit : does not continue ...
5
votes
Accepted
Grep line containing a word OR has length in a specific range?
With grep -E for Extended regexps you can use alternation (|).
$ grep -E 'CSCO|^.{0,15}$' file
1598427@931
CSCO 220715C00090000 ohlc=0,0,0,0 vol=0 oi=739 nbbo=0@0/4@1056 nbbo2=0@0/4@121
CSCO ...
5
votes
Grep line containing a word OR has length in a specific range?
grep -e CSCO -e '^.\{0,15\}$' filename
Here you give grep two patterns to look for. The first is "CSCO": so it'll match any lines with that. The second looks for the start of the line ^ ...
5
votes
get my file by awk - split by last occurrence
I'd use sed here:
sed -E 's/(.*)_/\1 /' file | column -t
4
votes
awk/sed find indexes of the first and the last capital letter in a string
$ awk '
match($0,/[[:upper:]](.*[[:upper:]])?/) {
print $0, RSTART, length()-(RSTART+RLENGTH-2)
}
' file
xyzAb 4 2
--AbbbAnde--- 3 7
abksjiRNNBBKUGFLYFYLF 7 1
-ankNUGUYUBUIGCafrg-- 5 7
...
4
votes
Accepted
awk/sed find indexes of the first and the last capital letter in a string
awk '
{
start = match($0, /[A-Z]/)
end = match($0, /[A-Z][^A-Z]*$/)
print (start ? start : "NaN"), (end ? length() - end + 1 : "NaN")
}' infile
4
votes
Accepted
Finding the line number of first occurrence of a text in bash script
You need to tell grep about your “that should be in the start of a line” constraint, by anchoring the match to the start of a line with ^:
trimLineNum=$(grep -m1 -n -- '^c' "${varFileLog}")
...
3
votes
Accepted
replace text in specific field with variables from loop
awk 'BEGIN{ FS=OFS="|" }
NR==FNR { id[$1, $2]=$3; next }
{ $4=( ($1, $4) in id? id[$1, $4]: $4) } 1' src dest
FS: Field Seperator
OFS: Output Field Seperator
NR==FNR: An ...
3
votes
awk/sed find indexes of the first and the last capital letter in a string
It is easy to get the length of the leading or trailing part with AWK. Add 1 to get the index as shown in the question.
echo '--AbbbAnde---
abksjiRNNBBKUGFLYFYLF
-ankNUGUYUBUIGCafrg--
BNKJUGFVULNK-Kew-...
2
votes
awk/sed find indexes of the first and the last capital letter in a string
Using Raku (formerly known as Perl_6)
Sample Input:
--AbbbAnde---
abksjiRNNBBKUGFLYFYLF
-ankNUGUYUBUIGCafrg--
BNKJUGFVULNK-Kew---
nouppercaseletters
oneUppercaseletter
Skips lines with 0 or 1 ...
2
votes
Accepted
awk get column value from file 2 for file 1 if value matches in 2 columns
There are two problems. First, you have a typo:
if (a[FNR] = $4)
That will always be true since you're using =, the assignment operator instead of == for comparison. You need:
if (a[FNR] == $4)
The ...
2
votes
Using AWK to sum up two column for each block
$ cat tst.awk
BEGIN { FS=SUBSEP=";"; OFS=" = " }
NF == 0 { prt(); delete sum }
{ print }
NF > 3 { sum[$4,$5]++ }
END { prt() }
function prt( key) {
for ( key in sum ) {
...
1
vote
Awk substr index 0
From the GNU awk online documentation: 'substr() function':
If start is less than one, substr() treats it as if it was one. (POSIX
doesn’t specify what to do in this case: BWK awk acts this way, and
...
1
vote
How to get lines which are unique for 1 column but are same for another column?
<infile sort -t'|' -k3,4 -k1,2 |
awk -F'|' '
($3==p[3] && $4==p[4] && $1==p[1] && $2!=p[2] ) &&
NR>1{ print buf ORS $0; next }
{ buf=$0; split($...
1
vote
awk/sed find indexes of the first and the last capital letter in a string
POSIX awk with field separator as an uppercase regex.
LC_ALL=C \
awk -F '[A-Z]' '
NF>2{
print length("x"$1), length("x"$NF)
}' file
Perl has index & rindex builtins to ...
1
vote
awk/sed find indexes of the first and the last capital letter in a string
Another perl approach:
perl -Mopen=locale -lne '
print 1+length$`, " ", 1+length$'\'' if /\p{Lu}(.*\p{Lu})?/'
\p{Lu} matches an upper case Letter (such as ABCÀÁÂÃÄÅАБВГᏢᏣᏤᏥ𝓐𝓑𝓒ⰗⰘⰙⱠⱢⱣⱤⱧⱩ....
1
vote
replace text in specific field with variables from loop
Another awk solution, which assumes that the lines from src will be used exactly once each and in order. This allows us to only keep track of the next line from src until it has been used, and then ...
1
vote
awk/sed find indexes of the first and the last capital letter in a string
{
s = match($0, /[A-Z]/); pad = RSTART
clone = substr($0, RSTART + 1)
while (match(clone, /[A-Z]/)) {
clone = substr(clone, RSTART + 1)
pad += RSTART
}
print $0, &...
1
vote
Accepted
Add information to HTML table with awk
Best I can tell this is all you need, i.e. 1 call to free -m, 1 call to awk, and no call to sed:
free -m |
awk '
BEGIN { print("<table border=1>") }
{
if ( NR == 1 ) {
...
1
vote
Combining files column-wise by first column as key (using grep or awk, etc.)
Having converted spaces in 2.txt back to tabs with sed -i 's/ */\t/' 2.txt,
awk -F '\t' '
BEGIN{OFS=FS} NR==FNR{F1[$1]=$2; next} {print $1, ($1 in F1)? F1[$1] : 0}
' 2.txt 1.txt
1 0
2 a
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
awk × 8241text-processing × 2835
sed × 2696
grep × 1040
shell-script × 942
bash × 901
linux × 771
shell × 453
perl × 441
csv × 329
regular-expression × 324
scripting × 270
command-line × 180
text-formatting × 162
files × 143
cut × 136
join × 134
sort × 126
columns × 120
bioinformatics × 109
numeric-data × 84
string × 83
find × 78
date × 77
logs × 71