Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a file containing lines of the form
this is block 1
a 1 2 3
this is block 2
a 3 1 9
this is block 3
a 10 2 32
...
I would like to copy some of the lines and replace the beginning so that it becomes:
this is block 1
a 1 2 3
b 1 2 3
c 1 2 3
this is block 2
a 3 1 9
b 3 1 9
c 3 1 9
this is block 3
a 10 2 32
b 10 2 32
c 10 2 32
...
Not sure how to solve this with awk, sed or another elegant option.
Using sed or awk you can do it in this way:
$ cat data
this is block 1
a 1 2 3
this is block 2
a 3 1 9
this is block 3
a 10 2 32
$ sed -rn '/^a/{p;s/^a/b/;p;s/^b/c/;p;n};p' data
this is block 1
a 1 2 3
b 1 2 3
c 1 2 3
this is block 2
a 3 1 9
b 3 1 9
c 3 1 9
this is block 3
a 10 2 32
b 10 2 32
c 10 2 32
$ awk '{if($1=="a") {print;$1="b";print;$1="c";print} else {print}}' data
this is block 1
a 1 2 3
b 1 2 3
c 1 2 3
this is block 2
a 3 1 9
b 3 1 9
c 3 1 9
this is block 3
a 10 2 32
b 10 2 32
c 10 2 32
$
Related
I have 4 column data files which have approximately 100 lines. I'd like to substract every nth from (n+3)th line and print the values in a new column ($5). The column data has not a regular pattern for each column.
My sample file:
cat input
1 2 3 20
1 2 3 10
1 2 3 5
1 2 3 20
1 2 3 30
1 2 3 40
1 2 3 .
1 2 3 .
1 2 3 . (and so on)
Output should be:
1 2 3 20 0 #(20-20)
1 2 3 10 20 #(30-10)
1 2 3 5 35 #(40-5)
1 2 3 20 ? #(. - 20)
1 2 3 30 ? #(. - 30)
1 2 3 40 ? #(. - 40)
1 2 3 .
1 2 3 .
1 2 3 . (and so on)
How can i do this in awk?
Thank you
For this I think the easiest thing is to read through the file twice. The first time (the NR==FNR block) we save all the 4th column values in an array indexed by the line number. The next block is executed for the second pass and creates a 5th column with the desired calculation (checking first to make sure that we wouldn't go passed the end of the file).
$ cat input
1 2 3 20
1 2 3 10
1 2 3 5
1 2 3 20
1 2 3 30
1 2 3 40
$ awk 'NR==FNR{a[NR]=$4; last=NR; next} {$5 = (FNR+3 <= last ? a[FNR+3] - $4 : "")}1' input input
1 2 3 20 0
1 2 3 10 20
1 2 3 5 35
1 2 3 20
1 2 3 30
1 2 3 40
You can do this using tac + awk + tac:
tac input |
awk '{a[NR]=$4} NR>3 { $5 = (a[NR-3] ~ /^[0-9]+$/ ? a[NR-3] - $4 : "?") } 1' |
tac | column -t
1 2 3 20 0
1 2 3 10 20
1 2 3 5 35
1 2 3 20 ?
1 2 3 30 ?
1 2 3 40 ?
1 2 3 .
1 2 3 .
1 2 3 .
I have a data file of:
1 2 3
1 5 7
2 5 9
11 21 110
6 17 -2
10 2 8
6 4 3
5 1 8
6 1 5
7 3 1
I want to add number 1 to the third column, only for line number 1, 3, 6, 8, 9, 10. And add 2 to the second column, for line number 6~9.
I know how to add 2 to entire second column, and add 1 to entire third column using awk
awk '{print $1, $2+2, $3+1}' data > data2
But how can I modify this code to specific lines of second and third column?
Thanks
Best,
awk to the rescue! You can check for NR in the condition, but for 6 values it will be tedious, alternatively you can check for string match with anchored NR.
$ awk 'BEGIN{lines=",1,3,6,8,9,10,"}
match(lines,","NR","){$3++}
NR>=6 && NR<=9{$2+=2}1' nums
1 2 4
1 5 7
2 5 10
11 21 110
6 17 -2
10 4 9
6 6 3
5 3 9
6 3 6
7 3 2
$ cat tst.awk
BEGIN {
for (i=6;i<=9;i++) {
d[2,i] = 2
}
split("1 3 6 8 9 10",t);
for (i in t) {
d[3,t[i]] = 1
}
}
{ $2 += d[2,NR]; $3 += d[3,NR]; print }
$ awk -f tst.awk file
1 2 4
1 5 7
2 5 10
11 21 110
6 17 -2
10 4 9
6 6 3
5 3 9
6 3 6
7 3 2
Hi I am trying to run a for loop with an increment counter that switches into an elif statement. The for loop is a way of building a string of syllables to synthesize with macintalk. I would like to add a short silence every 20ms but I cant seem to get it to work, I've tried a bunch of debugging steps but none seem to work. Can anyone spot the bug that prevents the elif from being accessed?
EDIT
Ok so I followed the suggestion below and used -eq instead = but I noticed that the counter only resets once and does not access the conditional statement a second time. The revised code is posted below:
counter=0;
for k in $indx
do
counter=$(($counter + 1));
echo 'increment counter'
echo $counter
if [ $k -eq 0 ]
then
stream=$stream'#_'${syllarray[k]}
elif [ $counter -eq 20 ]
then
echo Adding Silence after syllable: ${syllarray[k]}
stream=$stream'_'${syllarray[k]}'[[ slnc 20 ]]'
counter=0;
echo 'reset counter'
echo $counter
else
stream=$stream'_'${syllarray[k]}
fi
done
Sample output:
Synthesize A Syllable Stream with Predetermined Lexicon, Word Order and Phonology
Parameters:
Voice -- Victoria
Rate (words/min) -- 120
Pitch Modulation Interval -- 0
Baseline pitch -- 55
Directory Exists :)
Opening Syllable Transcription for Victoria
0
bIY
1
bUW
2
dAE
3
dOW
4
gOW
5
kUW
6
lAE
7
pAE
8
pIY
9
rOW
10
tIY
11
tUW
Counter Balanced Stimulus Order (Indexed by Syllables in Alphabetical Order)
11 2 9 8 4 6 0 5 10 11 2 9 8 4 6 1 3 7 8 4 6 0 5 10 11 2 9 1 3 7 0 5 10 8 4 6 1 3 7 0 5 10 8 4 6 11 2 9 0 5 10 1 3 7 8 4 6 11 2 9 1 3 7 11 2 9 0 5 10 1 3 7 8 4 6 0 5 10 11 2 9 8 4 6 1 3 7 0 5 10 8 4 6 11 2 9 0 5 10 8 4 6 11 2 9 0 5 10 1 3 7 8 4 6 1 3 7 0 5 10 11 2 9 1 3 7 8 4 6 0 5 10 1 3 7 11 2 9 1 3 7 11 2 9 1 3 7 0 5 10 8 4 6 1 3 7 0 5 10 11 2 9 0 5 10 11 2 9 8 4 6 11 2 9 1 3 7 8 4 6 0 5 10 1 3 7 11 2 9 8 4 6 0 5 10 8 4 6 1 3 7 11 2 9 0 5 10 1 3 7 8 4 6 11 2 9 8 4 6 1 3 7 11 2 9 0 5 10 8 4 6 11 2 9 0 5 10 8 4 6 11 2 9 1 3 7 0 5 10 1 3 7 8 4 6 0 5 10 1 3 7 0 5 10 11 2 9 1 3 7 11 2 9 8 4 6 0 5 10 11 2 9 8 4 6 1 3 7
Creating counterbalanced stimulus stream string with proper Macintalk formatting
increment counter
1
increment counter
2
increment counter
3
increment counter
4
increment counter
5
increment counter
6
increment counter
7
increment counter
8
increment counter
9
increment counter
10
increment counter
11
increment counter
12
increment counter
13
increment counter
14
increment counter
15
increment counter
16
increment counter
17
increment counter
18
increment counter
19
increment counter
20
Adding Silence after syllable: gOW
reset counter
0
increment counter
1
..
[truncated for clarity]
..
increment counter
268
Printing Stream to Screen
_tUW_dAE_rOW_pIY_gOW_lAE#_bIY_kUW_tIY_tUW_dAE_rOW_pIY_gOW_lAE_bUW_dOW_pAE_pIY_gOW[[ slnc 20 ]]_lAE#_bIY_kUW_tIY_tUW_dAE_rOW_bUW_dOW_pAE#_bIY_kUW_tIY_pIY_gOW_lAE_bUW_dOW_pAE#_bIY_kUW_tIY_pIY_gOW_lAE_tUW_dAE_rOW#_bIY_kUW_tIY_bUW_dOW_pAE_pIY_gOW_lAE_tUW_dAE_rOW_bUW_dOW_pAE_tUW_dAE_rOW#_bIY_kUW_tIY_bUW_dOW_pAE_pIY_gOW_lAE#_bIY_kUW_tIY_tUW_dAE_rOW_pIY_gOW_lAE_bUW_dOW_pAE#_bIY_kUW_tIY_pIY_gOW_lAE_tUW_dAE_rOW#_bIY_kUW_tIY_pIY_gOW_lAE_tUW_dAE_rOW#_bIY_kUW_tIY_bUW_dOW_pAE_pIY_gOW_lAE_bUW_dOW_pAE#_bIY_kUW_tIY_tUW_dAE_rOW_bUW_dOW_pAE_pIY_gOW_lAE#_bIY_kUW_tIY_bUW_dOW_pAE_tUW_dAE_rOW_bUW_dOW_pAE_tUW_dAE_rOW_bUW_dOW_pAE#_bIY_kUW_tIY_pIY_gOW_lAE_bUW_dOW_pAE#_bIY_kUW_tIY_tUW_dAE_rOW#_bIY_kUW_tIY_tUW_dAE_rOW_pIY_gOW_lAE_tUW_dAE_rOW_bUW_dOW_pAE_pIY_gOW_lAE#_bIY_kUW_tIY_bUW_dOW_pAE_tUW_dAE_rOW_pIY_gOW_lAE#_bIY_kUW_tIY_pIY_gOW_lAE_bUW_dOW_pAE_tUW_dAE_rOW#_bIY_kUW_tIY_bUW_dOW_pAE_pIY_gOW_lAE_tUW_dAE_rOW_pIY_gOW_lAE_bUW_dOW_pAE_tUW_dAE_rOW#_bIY_kUW_tIY_pIY_gOW_lAE_tUW_dAE_rOW#_bIY_kUW_tIY_pIY_gOW_lAE_tUW_dAE_rOW_bUW_dOW_pAE#_bIY_kUW_tIY_bUW_dOW_pAE_pIY_gOW_lAE#_bIY_kUW_tIY_bUW_dOW_pAE#_bIY_kUW_tIY_tUW_dAE_rOW_bUW_dOW_pAE_tUW_dAE_rOW_pIY_gOW_lAE#_bIY_kUW_tIY_tUW_dAE_rOW_pIY_gOW_lAE_bUW_dOW_pAE
Saving Synthesized Stream
Writing to: ./synthesis/stream-Victoria/stream.flac
I have a file test.txt with the following text
1 2 3 4
3 4 5 6
8 7 3 2
I want to save it as
4 3 2 1
6 5 4 3
2 3 7 8
Is there any shell command which does that?
rev will do the job:
rev file
4 3 2 1
6 5 4 3
2 3 7 8
I have a bunch of files from simulation output, all with the same number of rows and fields.
What I need to do is to combine them, so that I get only one file with the numbers summed up, which basically resembles the addition of several matrices.
Example:
File1.txt
1 1 1
1 1 1
1 1 1
File2.txt
2 2 2
2 2 2
2 2 2
File3.txt
3 3 3
3 3 3
3 3 3
required output
6 6 6
6 6 6
6 6 6
I'm going to integrate this into some larger Shell-script, therefore I would prefer a solution in awk, though other languages are welcome as well.
awk '{for(i=1;i<=NF;i++)a[FNR,i]=$i+a[FNR,i]}
END{for(i=1;i<=FNR;i++)
for(j=1;j<=NF;j++)printf "%s%s", a[i,j],(j==NF?"\n":FS)}' f1 f2 f3
input files could be more than 3
test with your data:
kent$ head f[1-3]
==> f1 <==
1 1 1
1 1 1
1 1 1
==> f2 <==
2 2 2
2 2 2
2 2 2
==> f3 <==
3 3 3
3 3 3
3 3 3
kent$ awk '{for(i=1;i<=NF;i++)a[FNR,i]=$i+a[FNR,i]}END{for(i=1;i<=FNR;i++)for(j=1;j<=NF;j++)printf "%s%s", a[i,j],(j==NF?"\n":FS)}' f1 f2 f3
6 6 6
6 6 6
6 6 6
Quick hack:
paste f1 f2 f3 | awk '{for(i=1;i<=m;i++)printf "%d%s",$i+$(i+m)+$(i+2*m),i==m?ORS:OFS}' m=3