# Shell Scripting

**URL:** <https://community.unix.com/t/shell-scripting/262417>\
**Category:** Shell Programming and Scripting\
**Created:** [March 24, 2010, 12:52pm UTC](https://community.unix.com/t/shell-scripting/262417 "2010-03-24T12:52:14Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![wawa](https://community.unix.com/letter_avatar/wawa/32/6_449c533f586939b6e5f246632a46421f.png) [@wawa](https://community.unix.com/u/wawa)\
**Post date:** [March 24, 2010, 12:52pm UTC](https://community.unix.com/t/shell-scripting/262417/1 "2010-03-24T12:52:14Z")

</div>

I'm trying to write a unix script to handle this function  
we are in the ksh env, but force bash in scripting  
I have three files as input and need to concatenate them onto one line at a time

For example: The first record of each file is  
File 1 =a  
File 2 =b  
File 3 =c

Output should be a;b;c

Second line of each file is

File 1 =d  
File 2 =e  
File 3 =f

output should be d;e;f

I would like to eventually open this file in excel. That is why I added the delimiters.

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [March 24, 2010, 1:05pm UTC](https://community.unix.com/t/shell-scripting/262417/2 "2010-03-24T13:05:06Z")

</div>

```nohighlight
awk -F= '{ORS=NR%3?";":"RS"}' file > file.csv

```

---

<div class="post-metadata">

**Author:** ![wawa](https://community.unix.com/letter_avatar/wawa/32/6_449c533f586939b6e5f246632a46421f.png) [@wawa](https://community.unix.com/u/wawa)\
**Post date:** [March 24, 2010, 1:09pm UTC](https://community.unix.com/t/shell-scripting/262417/3 "2010-03-24T13:09:37Z")

</div>

not familiar with awk

can you tell me what this is doing or how do I represent my files

---

<div class="post-metadata">

**Author:** ![phoenix\_nebula](https://community.unix.com/user_avatar/community.unix.com/phoenix_nebula/32/1819_2.png) [@phoenix\_nebula](https://community.unix.com/u/phoenix_nebula)\
**Post date:** [March 24, 2010, 1:29pm UTC](https://community.unix.com/t/shell-scripting/262417/4 "2010-03-24T13:29:34Z")

</div>

```nohighlight
cut -f2 -d"=" sample.txt | tr "\n" ";"

```

cut command gives the secong coloumn with delimiter "="

tr -- translates the \n to ;

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [March 24, 2010, 1:49pm UTC](https://community.unix.com/t/shell-scripting/262417/5 "2010-03-24T13:49:06Z")

</div>

> [@wawa](#):
>
> not familiar with awk
> 
> can you tell me what this is doing or how do I represent my files

Sorry I forgot to print the values:

```plaintext
awk -F= '{ORS=NR%3?";":RS}{print $2}' file > file.csv

```

---

<div class="post-metadata">

**Author:** ![wawa](https://community.unix.com/letter_avatar/wawa/32/6_449c533f586939b6e5f246632a46421f.png) [@wawa](https://community.unix.com/u/wawa)\
**Post date:** [March 24, 2010, 2:35pm UTC](https://community.unix.com/t/shell-scripting/262417/6 "2010-03-24T14:35:31Z")

</div>

I think I've misrepresented my input files. I have three files with data in them. I want to string the data in those files together on one line, so I tried a cat statement, but it just lists all the files end to end. I need unix to read one record from each file and string them together on one line with delimeters, then go to the next line on each input file and create a new line on the output file with second record. Each file has approx 10,000 lines on it. All the reconstructed records need to be on one output file in a csv format  
sorry for the confusion or maybe I'm just confused on what you guys are showing me

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [March 24, 2010, 3:04pm UTC](https://community.unix.com/t/shell-scripting/262417/7 "2010-03-24T15:04:32Z")

</div>

```nohighlight
awk '{getline a < "file2";getline b < "file3";print $0,a,b}' OFS=";" file1 > file.csv

```

---

<div class="post-metadata">

**Author:** ![wawa](https://community.unix.com/letter_avatar/wawa/32/6_449c533f586939b6e5f246632a46421f.png) [@wawa](https://community.unix.com/u/wawa)\
**Post date:** [March 24, 2010, 3:36pm UTC](https://community.unix.com/t/shell-scripting/262417/8 "2010-03-24T15:36:30Z")

</div>

I just replaced your file names with my actual names and got some errors.

```nohighlight
awk '{getline a < "scomp2p8_2010_03_01.txt";getline b < "scomp2p6_2010_03_01.txt";print $0,a,b}' OFS=";" scomp2p5_2010_03_01.txt > scomp2p9_2010_03_01.txt''

awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: illegal statement near line 1

```

---------- Post updated at 03:36 PM ---------- Previous update was at 03:34 PM ----------

[/COLOR]that double quote at the end isn't actually part of the awk statement

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [March 25, 2010, 3:13am UTC](https://community.unix.com/t/shell-scripting/262417/9 "2010-03-25T03:13:43Z")

</div>

Try nawk or /usr/xpg4/bin/awk on Solaris.

---

<div class="post-metadata">

**Author:** ![wawa](https://community.unix.com/letter_avatar/wawa/32/6_449c533f586939b6e5f246632a46421f.png) [@wawa](https://community.unix.com/u/wawa)\
**Post date:** [March 25, 2010, 11:38am UTC](https://community.unix.com/t/shell-scripting/262417/10 "2010-03-25T11:38:12Z")

</div>

I got it to work with nawk thanks, but for some reason when I replace the hardcoded file names with variables. It won't accept RUNDATE variable.

RUNDATE="2010\_03\_24";

nawk '{getline a \< "scomp2p8\_${RUNDATE}.txt";getline b \< "scomp2p6\_${RUNDATE}.txt";print $0,a,b}' OFS=";" scomp2p5\_${RUNDATE}.txt \> scomp2p9\_2010\_03\_24.txt

Also the usr/xpg4/bin/awk seems to exist on the box, but this statement won't work. Just curious why?

usr/xpg4/bin/awk '{getline a \< "scomp2p8\_2010\_03\_01.txt";getline b \< "scomp2p6\_2010\_03\_01.txt";print $0,a,b}' OFS=";" scomp2p5\_2010\_03\_01.txt \> scomp2p9\_2010\_03\_01.txt

bash: usr/xpg4/bin/awk: No such file or directory

thanks for all the help

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [March 25, 2010, 2:09pm UTC](https://community.unix.com/t/shell-scripting/262417/11 "2010-03-25T14:09:57Z")

</div>

> [@wawa](#):
>
> I got it to work with nawk thanks, but for some reason when I replace the hardcoded file names with variables. It won't accept RUNDATE variable.
> 
> RUNDATE="2010\_03\_24";
> 
> nawk '{getline a \< "scomp2p8\_${RUNDATE}.txt";getline b \< "scomp2p6\_${RUNDATE}.txt";print $0,a,b}' OFS=";" scomp2p5\_${RUNDATE}.txt \> scomp2p9\_2010\_03\_24.txt

With shell variables the command should be:

```plaintext
nawk -v rd=${RUNDATE} '{getline a < "scomp2p8_"rd".txt";getline b < "scomp2p6_"rd".txt";print $0,a,b}' OFS=";" scomp2p5_${RUNDATE}.txt > scomp2p9_2010_03_24.txt

```

> [@wawa](#):
>
> Also the usr/xpg4/bin/awk seems to exist on the box, but this statement won't work. Just curious why?
> 
> usr/xpg4/bin/awk '{getline a \< "scomp2p8\_2010\_03\_01.txt";getline b \< "scomp2p6\_2010\_03\_01.txt";print $0,a,b}' OFS=";" scomp2p5\_2010\_03\_01.txt \> scomp2p9\_2010\_03\_01.txt
> 
> bash: usr/xpg4/bin/awk: No such file or directory
> 
> thanks for all the help

You forgot the slash before usr.

---

<div class="post-metadata">

**Author:** ![durden\_tyler](https://community.unix.com/user_avatar/community.unix.com/durden_tyler/32/1450_2.png) [@durden\_tyler](https://community.unix.com/u/durden_tyler)\
**Post date:** [March 25, 2010, 3:06pm UTC](https://community.unix.com/t/shell-scripting/262417/12 "2010-03-25T15:06:46Z")

</div>

... deleted ... redundant solution...

---

<div class="post-metadata">

**Author:** ![wawa](https://community.unix.com/letter_avatar/wawa/32/6_449c533f586939b6e5f246632a46421f.png) [@wawa](https://community.unix.com/u/wawa)\
**Post date:** [March 25, 2010, 3:22pm UTC](https://community.unix.com/t/shell-scripting/262417/13 "2010-03-25T15:22:54Z")

</div>

still doesn't recognize the rd variable. doesn't read in the files within the double quotes.

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [March 25, 2010, 5:34pm UTC](https://community.unix.com/t/shell-scripting/262417/14 "2010-03-25T17:34:22Z")

</div>

```nohighlight
nawk -v f1="scomp2p8_"${RUNDATE}".txt" -v f2="scomp2p6_"$(RUNDATE)".txt" '{getline a < f1;getline b < f2;print $0,a,b}' OFS=";" scomp2p5_${RUNDATE}.txt > scomp2p9_2010_03_24.txt

```

---

<div class="post-metadata">

**Author:** ![wawa](https://community.unix.com/letter_avatar/wawa/32/6_449c533f586939b6e5f246632a46421f.png) [@wawa](https://community.unix.com/u/wawa)\
**Post date:** [March 25, 2010, 6:36pm UTC](https://community.unix.com/t/shell-scripting/262417/15 "2010-03-25T18:36:57Z")

</div>

Thanks that worked. I don't know who came up with this idea of sharing the knowledge, but I love it.

Thanks big time

Hopefully sometime I can return the favor
