# Shell Scripting

**URL:** <https://community.unix.com/t/shell-scripting/289829>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [June 15, 2011, 4:54am UTC](https://community.unix.com/t/shell-scripting/289829 "2011-06-15T04:54:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sampandey31](https://community.unix.com/letter_avatar/sampandey31/32/6_449c533f586939b6e5f246632a46421f.png) [@sampandey31](https://community.unix.com/u/sampandey31)\
**Post date:** [June 15, 2011, 4:54am UTC](https://community.unix.com/t/shell-scripting/289829/1 "2011-06-15T04:54:19Z")

</div>

Hey I have a data in the file named as outputFile.txt. The data is in the format  
123456,12345678912345,400,09/09/09,INACTIVE.

I want this output without commas ie

12345612345678912345400090909INACTIVE.

Please tell me what to do and clear explain all the terms, as I am new to it.

---

<div class="post-metadata">

**Author:** ![itkamaraj](https://community.unix.com/user_avatar/community.unix.com/itkamaraj/32/1880_2.png) [@itkamaraj](https://community.unix.com/u/itkamaraj)\
**Post date:** [June 15, 2011, 5:05am UTC](https://community.unix.com/t/shell-scripting/289829/2 "2011-06-15T05:05:11Z")

</div>

```nohighlight
bash-3.00$ nawk -F"," '{ for (i=1;i<=NF;i++) {if(i==NF) {printf("%s\n",$NF)} else printf ("%s",$i)}}' /tmp/myfile
1234561234567891234540009/09/09INACTIVE
1234561234567891234540009/09/09INACTIVE
1234561234567891234540009/09/09INACTIVE

bash-3.00$ cat /tmp/myfile
123456,12345678912345,400,09/09/09,INACTIVE
123456,12345678912345,400,09/09/09,INACTIVE
123456,12345678912345,400,09/09/09,INACTIVE

```

read each word (seperated by comma) and just print it. If it is the last word, then print new line character ("\n")

---------- Post updated at 02:35 PM ---------- Previous update was at 02:33 PM ----------

you dont want / (forward slash) also ?

---

<div class="post-metadata">

**Author:** ![sampandey31](https://community.unix.com/letter_avatar/sampandey31/32/6_449c533f586939b6e5f246632a46421f.png) [@sampandey31](https://community.unix.com/u/sampandey31)\
**Post date:** [June 15, 2011, 5:06am UTC](https://community.unix.com/t/shell-scripting/289829/3 "2011-06-15T05:06:20Z")

</div>

Hey itkamaraj  
Thanks!!

Can u explain me what is F and NF and whats the logic behind this.

---

<div class="post-metadata">

**Author:** ![itkamaraj](https://community.unix.com/user_avatar/community.unix.com/itkamaraj/32/1880_2.png) [@itkamaraj](https://community.unix.com/u/itkamaraj)\
**Post date:** [June 15, 2011, 5:14am UTC](https://community.unix.com/t/shell-scripting/289829/4 "2011-06-15T05:14:00Z")

</div>

or simply use the sed command

```plaintext
 
bash-3.00$ sed 's/,//g' /tmp/myfile
1234561234567891234540009/09/09INACTIVE
1234561234567891234540009/09/09INACTIVE
1234561234567891234540009/09/09INACTIVE

```

---------- Post updated at 02:44 PM ---------- Previous update was at 02:41 PM ----------

NF - Number of Fields  
[Awk - A Tutorial and Introduction - by Bruce Barnett](http://www.grymoire.com/Unix/Awk.html#uh-17)  
F - Field Seperator  
[Awk - A Tutorial and Introduction - by Bruce Barnett](http://www.grymoire.com/Unix/Awk.html#uh-15)

---

<div class="post-metadata">

**Author:** ![sampandey31](https://community.unix.com/letter_avatar/sampandey31/32/6_449c533f586939b6e5f246632a46421f.png) [@sampandey31](https://community.unix.com/u/sampandey31)\
**Post date:** [June 15, 2011, 5:17am UTC](https://community.unix.com/t/shell-scripting/289829/5 "2011-06-15T05:17:05Z")

</div>

Hey thanks, but both are not working  
Below is the code.  
In the code I am asking the user to enter the Location Code and Account number and status, then I am using Loaction and Account Number to match into a inputFile.txt and then writing the data to an output file. Also I have to write 400, date and status at the end of the line.

```nohighlight
#!/bin/sh
#Author : Sameer Pandey
#Copyrights: Anybody can modify and make it work better.

echo "Input Location Code"
read locationCode

echo "Input the Account number"
read accountNumber

echo "Input the Status"
read status

>outFile.txt
chmod 777 outFile.txt
>outputFile.txt
chmod 777 outputFile.txt
egrep -i "$locationCode" inputFile.txt > tempFile.txt
grep -i "$accountNumber" tempFile.txt > outFile.txt

rm tempFile.txt

echo "400" >> outFile.txt
date "+%y/%m/%d" >> outFile.txt
echo $status >> outFile.txt

awk 'ORS=NR%1?RS:","' RS="\n" outFile.txt >> outputFile.txt

sed 's/,//g' outputFile.txt

cat outputFile.txt >> outputFilefinal.txt

echo "Processes Completed successfully"

```

Kindly look at the code and tell me where is the error.

---

<div class="post-metadata">

**Author:** ![itkamaraj](https://community.unix.com/user_avatar/community.unix.com/itkamaraj/32/1880_2.png) [@itkamaraj](https://community.unix.com/u/itkamaraj)\
**Post date:** [June 15, 2011, 5:42am UTC](https://community.unix.com/t/shell-scripting/289829/6 "2011-06-15T05:42:35Z")

</div>

post the outFile.txt contents

use sed -i

```nohighlight
 
sed -i 's/,//g' outputFile.txt

```

---

<div class="post-metadata">

**Author:** ![sampandey31](https://community.unix.com/letter_avatar/sampandey31/32/6_449c533f586939b6e5f246632a46421f.png) [@sampandey31](https://community.unix.com/u/sampandey31)\
**Post date:** [June 15, 2011, 5:44am UTC](https://community.unix.com/t/shell-scripting/289829/7 "2011-06-15T05:44:53Z")

</div>

> [@itkamaraj](#):
>
> post the outFile.txt contents
> 
> use sed -i
> 
> ```plaintext
>  
> sed -i 's/,//g' outputFile.txt
> 
> ```

Thank You very very much. It worked. 🙂 🙂

I just wanted to ask one thing more.  
If suppose I wanted to read the file until I encounter a comma, then write that data into another file. How will I do that?
