ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
And so on.
I have numerous number of such file. What I am trying to do is take 6 and 7th column of them(which I need), but also within a file that takes tha AuCr or Au-Au into account.
What I have done by now is it takes all the $6 and $7 (awk one-liner). How can I write it in a output which contains the Au-Au or Au-Cr in it?
One thing is there which is not evident in the file I gave:
the name of components(Au-Au/Au-Cr etc) is the first letter in the line where the rest of the data
1 1 1.000 0.500 0.500 ( 1.22474) -0.00048 -0.00048
has a blank space at the beginning.....so that may help.....but i am clueless. plz help
no...actually it may vary by Au-Cr, Au-Au, Cr-Au, Cr-Cr.
And I want to get the all lines below such Au-Cr combination until another such combination comes in and do the same until the end of the file.
while read line; do
if line begins with <Regex1>; then
echo $line
if line begins with <Regex2>; then
echo $line | awk '{print $6 #7}'
done < filename > newfilename
Regex1 to match Au-Cr, etc. (This might be simply "^[a-zA-z]" (all lines starting with a letter) OR "[AC]" (all lines starting with "A" or "C")
Regex1 to match the lines where you want columns 6 and 7--eg "^1 1" (all lines beginning with "1 1"
The whole thing can be written in AWK, if desired, or in BASH + calling AWK for the columns
Something like this may work. Save it as a bash script and when you run it pass it the name of the data file you want to process as the first argument. Eg, save it as data.bash then run it with
bash data.bash /path/to/data.file
Code:
#!/bin/bash
set -e
count=0
while read myline
do
# turn myline into an array so we can work on different fields
line=($myline)
if [[ ${line[0]} = Au ]] && [[ ${line[1]} = -Au || ${line[1]} = -Cr ]]
# thing is a temporary variable to hold "Au -Au" or "Au -Cr"
then thing="${line[0]} ${line[1]}"
fi
echo $thing ${line[6]/)/} ${line[7]}
((++count))
done < $1
Last edited by Andrew Benton; 06-29-2010 at 11:16 AM.
Reason: Removed an unneeded sed and added some comments
I am sorry but you are not getting my point.
what you have defined as $thing should be a file and line(6) and line(7) are the data written in the file.
I tried :
Code:
#!/bin/bash
set -e
count=0
while read myline
do
# turn myline into an array so we can work on different fields
line=($myline)
if [[ ${line[0]} = Au ]] && [[ ${line[1]} = -Au || ${line[1]} = -Cr ]]
# thing is a temporary variable to hold "Au -Au" or "Au -Cr"
then thing="${line[0]} ${line[1]}"
# echo $thing
fi
# echo $thing
print ${line[6]/)/} ${line[7]} >$thing
((++count))
done < $1
which is giving error:
Code:
$ ./proc.sh test_prn
./proc.sh: line 15: $thing: ambiguous redirect
#!/bin/bash
set -e
thing=/dev/null
echo > "Au -Au"
echo > "Au -Cr"
echo > "Cr -Au"
echo > "Cr -Cr"
count=0
while read myline
do
# turn myline into an array so we can work on different fields
line=($myline)
if [[ ${line[0]} = Au || ${line[0]} = Cr ]] &&
[[ ${line[1]} = -Au || ${line[1]} = -Cr ]]
#
# thing is a temporary variable to hold
# "Au -Au", "Au -Cr", "Cr -Au" or "Cr -Cr"
#
then thing="${line[0]} ${line[1]}"
fi
echo ${line[6]/)/} ${line[7]} >> "$thing"
((++count))
done < $1
Last edited by Andrew Benton; 06-29-2010 at 05:49 PM.
Andrew,
Thanks a lot. But I am not understanding how it reads the filenames.
actually, I have directories :
Quote:
$ ls -d */
Au10-Cr90/ Au15-Cr85/ Au20-Cr80/ Au25-Cr75/ Au5-Cr95/
inside which the working file is for each directory. Hence I tried:
Code:
#!/bin/bash
set -e
thing=/dev/null
echo > "Au -Au"
echo > "Au -Cr"
echo > "Cr -Au"
echo > "Cr -Cr"
c_min=$1
c_max=$2
c_gap=$3
for (( i = $c_min; i <= $c_max; i = i +$c_gap))
do
ca=$i
cb=`echo "100-$i"|bc`
tail -103 Au${ca}-Cr${cb}/jij/AuCr01.prn >./tmp
#awk '{print $7," " $8}' tmp >jij
count=0
while read myline
do
# turn myline into an array so we can work on different fields
line=($myline)
if [[ ${line[0]} = Au || ${line[0]} = Cr ]] &&
[[ ${line[1]} = -Au || ${line[1]} = -Cr ]]
#
# thing is a temporary variable to hold
# "Au -Au", "Au -Cr", "Cr -Au" or "Cr -Cr"
#
then thing="${line[0]} ${line[1]}"
fi
echo ${line[6]/)/} ${line[7]} >> "jij_Au$ca-$thing"
((++count))
done < $1
#awk '{sub (/)/,"");print}' jij>jij_Au$ca
done
and run this code as :
Code:
$ ./new_jij.sh 5 25 5
./new_jij.sh: line 35: 5: No such file or directory
./new_jij.sh: line 35: 5: No such file or directory
./new_jij.sh: line 35: 5: No such file or directory
./new_jij.sh: line 35: 5: No such file or directory
./new_jij.sh: line 35: 5: No such file or directory
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.