Search Results

Search: Posts Made By: littlewenwen
2,722
Posted By Chubler_XL
Slight change to Yoda's solution to replace ID...
Slight change to Yoda's solution to replace ID within file.

Note this should also work find with /bin/bash (if you don't have ksh installed).

#!/bin/ksh

file="file_000"

while read id...
2,722
Posted By bartus11
Try:awk 'NR==FNR{a[NR]=$0;n=NR;next}{for...
Try:awk 'NR==FNR{a[NR]=$0;n=NR;next}{for (i=1;i<=n;i++){x=a[i];gsub("000",$0,x);print x >> "file_"$0}}' file_000 file_id
2,722
Posted By Yoda
#!/bin/ksh file="file_000" while read...
#!/bin/ksh

file="file_000"

while read id
do
cp "$file" "${file%_*}_$id"
done < file_id
1,139
Posted By Corona688
See Advanced Bash Scripting Guide -- String...
See Advanced Bash Scripting Guide -- String Operations (http://tldp.org/LDP/abs/html/refcards.html#AEN22664)

It matches the longest match of "*abc2" from the start of the string, and replaces it...
1,139
Posted By Corona688
From man expr: index STRING CHARS ...
From man expr:

index STRING CHARS
index in STRING where any CHARS is found, or 0

I don't think expr can do that.

You can do that with replacement and some arithmetic:
...
3,314
Posted By hanson44
That's a little tricky. I would suggest the...
That's a little tricky. I would suggest the following as the best simple solution:
$ var999=abc$'\n'def$'\n'defabc$'\n'def$'\n'def
$ echo -n "$var999" | wc -l
4

Reports the right number of...
3,314
Posted By Yoda
The echo command writes its argument to standard...
The echo command writes its argument to standard output, followed by a <newline>. If there are no arguments, only the <newline> is written.
$ echo | od -c
0000000 \n
0000001

This is why you...
3,314
Posted By hanson44
The variable does not have a newline. The...
The variable does not have a newline.

The echo command adds the newline, which "wc -m" counts. That's why you get a different count with "echo -n", which does not add a newline.
4,416
Posted By hanson44
Yes, you can use the second field alone, using...
Yes, you can use the second field alone, using cut. Here is how I would do it, which seems simple and clear.
n=0
cut -f 2 myfile > fld2.txt
while read fld_2; do
array_var[n]=$fld_2
n=`expr...
1,534
Posted By elixir_sinari
Don't know about other awk implementations but...
Don't know about other awk implementations but gawk does have these egrep like regexp characters. From man gawk:
\y matches the empty string at either the beginning or the end
...
1,534
Posted By balajesuri
In perl, the word anchor is \b
In perl, the word anchor is \b
5,604
Posted By alister
For any questions regarding special perl...
For any questions regarding special perl variables, the perlvar (http://perldoc.perl.org/perlvar.html) man page is the definitive source.

Regards,
Alister
5,604
Posted By anbu23
Try $.
Try $.
14,030
Posted By Scrutinizer
You can also do something like this: awk...
You can also do something like this:
awk '{A[$1,$2]=$3; I[$1]; J[$2]} END{ for(i in I)for(j in J) print i, j, A[i,j]}' file
or for example:
awk '{A[$1,$2]=$3; J[$2]} END{ for(j in J) print 5, j,...
14,030
Posted By Corona688
awk does not actually have two-dimensional...
awk does not actually have two-dimensional arrays; it handles two indexes by squeezing them into a single string. As such, your first loop will work but may look weird; the SUBSEP character which...
1,606
Posted By mirni
With perl you can specify a backup extension...
With perl you can specify a backup extension after the -i switch:
perl -i.bak 's/:\S*//g' file #will save the original as file.bakGNU sed can also edit in-place, here is sed solution:
sed -i 's/:[^...
1,606
Posted By MadeInGermany
Yes, perl -i ... does it. Here perl seems to be...
Yes, perl -i ... does it.
Here perl seems to be preferable anyway.
perl -pe 's/:\S*//g' file
1,606
Posted By MadeInGermany
There is no default print if the main loop...
There is no default print if the main loop consists of only {}
You must add a print command. Lazy people do a 1, that's a true condition where the default action is again a print.
For demonstration...
1,606
Posted By Yoda
Another method: awk '{ gsub(/:[^ ]*/,x); $1=$1...
Another method:
awk '{ gsub(/:[^ ]*/,x); $1=$1 }1' input_file > input_file.tmp

mv input_file.tmp input_file
1,606
Posted By mirni
Awk is a filter, it doesn't modify the input. You...
Awk is a filter, it doesn't modify the input. You should redirect into a temporary file, then overwrite the original.
How about:

cp $input ${input}.copy
awk -F: '{print $1}' RS="[ \t]+" ORS=" "...
1,291
Posted By jim mcnamara
You cannot embed shell variables in an awk...
You cannot embed shell variables in an awk statement that way.
Generally

var=foo
awk -v myvar=$var '{.....}' somefile

-v [awkvariable name]=$variable

It looks like you are trying to get a...
1,291
Posted By Yoda
You have to assign an external variable to awk...
You have to assign an external variable to awk variable to start working on it.

No need to use /dev/null , you can print inside BEGIN block.

Also starting character in substr function is...
1,598
Posted By Yoda
That is because when you used $d you were...
That is because when you used $d you were actually assigning NULL to your array: myarray for 3 consecutive times.

This is the reason why it was printing NULL followed by number of occurrence: 3....
1,598
Posted By balajesuri
Please explain the logic that goes in to get the...
Please explain the logic that goes in to get the expected output. Merely looking at your input and output, I could suggest this:
ls -1 | awk 'BEGIN { i=1 } {print i, $0; i++}'
1,598
Posted By Yoda
awk 'BEGIN{while("ls"|getline d) myarray[++j]=d;...
awk 'BEGIN{while("ls"|getline d) myarray[++j]=d; close("ls")}END{ for (i in myarray){print i, myarray[i]}}' /dev/null
Showing results 1 to 25 of 32

 
All times are GMT -4. The time now is 07:13 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy