The time now is Sat 18 May 2013, 22:39
All times are UTC - 4 |
| Author |
Message |
acampbe2
Joined: 11 Jun 2012 Posts: 2
|
Posted: Mon 11 Jun 2012, 22:49 Post subject:
Having trouble with bash syntax logic |
|
0 down vote favorite
share [g+] share [fb] share [tw]
I need help reading a txt file line by line, and then looping through each element in bash script. After hitting a element I need to cut the # of the column represented by the current numerical element, and then assign it to a file. In the end I need to paste all these files together. The columns are in a separate file. The first txt file reads as such
1,6,62,6
7,7,52,24
3,145,25,69
Then for example I need to cut column 5 in a separate file, and then assign to a new file. One the line is done the process starts again.This process would be easy in python or java, but I'm just very new to bash script.
This is a sample column:
1:45
1:56
1:56
1:69
ccData_t.nnv
while read line; do
cut -f2 ccData_t.nnv >p
for i in $(line);
do
cut -fi+1 ccData_t.nnv |sed "s/^/"i":/" >ti
echo paste p t1 t2 t3 t4 |grep -v pro >svm_train_model1.tsv
done < FILE
In the end I have to have all columns appended together as such.
1:45 6:45 62:45
1:56 6:34 62:23
1:56 6:32 62:52
1:69 6:12 62:123
|
|
Back to top
|
|
 |
disciple
Joined: 20 May 2006 Posts: 6178 Location: Auckland, New Zealand
|
Posted: Mon 11 Jun 2012, 23:44 Post subject:
|
|
I see you have another thread:
http://www.murga-linux.com/puppy/viewtopic.php?t=78947
Or is this a howto after all?
If it isn't, it isn't immediately obvious exactly what you're asking for help with. Could you clarify?
_________________ DEATH TO SPREADSHEETS
- - -
Classic Puppy quotes
- - -
Beware the demented serfers!
|
|
Back to top
|
|
 |
Flash
Official Dog Handler

Joined: 04 May 2005 Posts: 9841 Location: Arizona USA
|
Posted: Tue 12 Jun 2012, 00:43 Post subject:
|
|
It looks to me like acampbe2 copied that from another forum and pasted it in this forum. He even left some junk from the other forum at the top.
|
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 4001 Location: Arizona, U.S.A.
|
Posted: Tue 12 Jun 2012, 04:54 Post subject:
|
|
Hi acampbe2;
| Code: | for L in `cat File.txt`
do
( Do what you need to...)
done
paste File-1.txt File-2.txt > Final-File.txt |
|
|
Back to top
|
|
 |
Karl Godt

Joined: 20 Jun 2010 Posts: 2675 Location: Kiel,Germany
|
Posted: Tue 12 Jun 2012, 08:37 Post subject:
|
|
Your problem is probably that column 5 is not always there :
1,6,62,6 has 4 columns ...
while read line;do
column_5=`echo "$line" |cut -f 5 -d ','`
if [ "$column_5" = '' ];then
column_5='0'
fi
done <FILE
| Code: | echo "
1,2,3,4
2,3,4,1,
4,3,2,1,,
5,4,3,2,1
" >test_file.lst
while read line;do [ "$line" ] || continue;column_5=`echo "$line" | cut -f 5 -d ','`; [ "$column_5" ] || column_5=0; echo "$line | $column_5"; done <test_file.lst |
| Quote: | 1,2,3,4 | 0
2,3,4,1, | 0
4,3,2,1,, | 0
5,4,3,2,1 | 1 |
|
|
Back to top
|
|
 |
Moose On The Loose

Joined: 24 Feb 2011 Posts: 278
|
Posted: Tue 12 Jun 2012, 10:33 Post subject:
|
|
I edited the Karl Godt comment a little:
| Karl Godt wrote: | Your problem is probably that column 5 is not always there :
1,6,62,6 has 4 columns ...
| Code: |
while read line;do
column_5=`echo "$line" |cut -f 5 -d ','`
if [ "$column_5" = '' ];then
column_5='0'
fi
done <FILE
|
|
There is a slightly easier way to deal with the issue of too few columns.
| Code: |
column_5=`echo "${line},0,0,0,0,0,0,0" |cut -f 5 -d ','`
|
This way cut always has the needed column.
|
|
Back to top
|
|
 |
Karl Godt

Joined: 20 Jun 2010 Posts: 2675 Location: Kiel,Germany
|
Posted: Tue 12 Jun 2012, 10:44 Post subject:
|
|
| Moose On The Loose wrote: | I edited the Karl Godt comment a little:
| Karl Godt wrote: | Your problem is probably that column 5 is not always there :
1,6,62,6 has 4 columns ...
| Code: |
while read line;do
column_5=`echo "$line" |cut -f 5 -d ','`
if [ "$column_5" = '' ];then
column_5='0'
fi
done <FILE
|
|
There is a slightly easier way to deal with the issue of too few columns.
| Code: |
column_5=`echo "${line},0,0,0,0,0,0,0" |cut -f 5 -d ','`
|
This way cut always has the needed column. |
But watch out $line has an entry. If $line would be NULL , then cut and gawk -F "," would treat ",1,2,3,4,5" differently . Field 1 would be NULL then and field 5 would get the value '4' .
|
|
Back to top
|
|
 |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|