Need to write a script to reformat a file in unix but not familiar with unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to write a script to reformat a file in unix but not familiar with unix
# 1  
Old 08-13-2009
Need to write a script to reformat a file in unix but not familiar with unix

unix script must do the fiollowing

open a file containing comma delimited records
> each record contains 10 fields
> removes the 2nd field and use that same field containing fields 2 to 10
the original record after fprocessing should containing fields 1 and 3
a new erecord must be created for each record if coloumn 2 has a value
if it doesnt have a value then just remove field from original record hence making
# 2  
Old 08-13-2009
Posting the source files (at least a few lines as a sample) and the output you desire would go a long way here.
# 3  
Old 08-13-2009
Is this a homework question?

Can you give an explanation of your real-world problem?
# 4  
Old 08-14-2009
see files attached file before is before conversion file after is what is expected after n:B if field obene of field two is empty then record must not be created
field one or field two must exist
n not a homework question trying to format a file before it goes into a perl program
so far this is what i have got
but it doesnt fit the criteria for when field one ot two is empty
i #!/bin/ksh
#
#
cnt=1
IFS=','
while read fld1 fld2 rest
do
if [ cnt -gt 1 ]; then
echo $fld1, $rest
echo $fld2, $rest
fi
cnt=`expr $cnt+1`
done < CustomerData.txt | sed 's/ /,/g'
# 5  
Old 08-14-2009
Assuming you have a header (1st line) in the file:

Code:
awk -F, 'NR==1 || NF < 10 {print;next}
{
 s1=$1; s2=$2
 $1=$2=""
 sub(",,",",")
 print s1 $0
 print s2 $0
}' OFS="," filebefore.txt > fileafter.txt

# 6  
Old 08-14-2009
Thanks franlyn
tried code but it still doesnt facilitate if field 1 or field two is empty
dont create record in that scenario .
Look at file attached
record 1 should only create one record starting with 90003371001

record 3 should only create 1 record with 12406071240597as firts feild
# 7  
Old 08-14-2009
Code:
awk -F, 'NR==1 || NF < 10 {print;next}
{
 s1=$1; s2=$2
 $1=$2=""
 sub(",,",",")
 if(s1) print s1 $0
 if(s2) print s2 $0
}' OFS="," file > new_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Write terminal contents into a one file in UNIX

Hi guys, How to write terminal contents into a file in Unix operating system Actually I created GUI by using Gtk2-perl. I want to display data on GUI whatever the contents writing on terminal. So which command I have to use and where that command to be run I mean in shell script or Perl... (2 Replies)
Discussion started by: kiran425
2 Replies

2. Shell Programming and Scripting

Need a UNIX/perl script to read and write the data

Hi, I have on Designdocument in that information is stored with in tabular format.I need Perl/unix script to read and write the data using perl script? Regards, Ravi (4 Replies)
Discussion started by: toravi.pentaho
4 Replies

3. Shell Programming and Scripting

How to write text file data to excel using UNIX shell script?

Hi All, I have the requirement in unix shell script. I want to write the "ls -ltr" command out put to excel file as below. Input :text file data : drwxr-xr-x 5 root root 4096 Oct 2 12:26 drwxr-xr-x 2 apx aim 4096 Nov 29 18:40 drwxr-xr-x 5 root root 4096 Oct 2 12:26 drwxr-xr-x... (10 Replies)
Discussion started by: Balasankar
10 Replies

4. Shell Programming and Scripting

How to write a script in unix to get value from SQR?

hi this is naga, i had created one SQR , but i didnt include the code to get the notification(failure or success ) thats way every time its returning the status=0 SQR was went to success(if it failed also), without touching the code of the SQR can any one help to write script in unix to get... (4 Replies)
Discussion started by: nagavenkatesh
4 Replies

5. Shell Programming and Scripting

Write an executable file in Unix

Hi, I want to write an executable file in unix env to go to a particular path instead of always typing the long path cd /app/oracle/product/10.2.0/Db_1/scripts/prejib/sample. I have tried with the below script in but not working . please help me bash-3.00$ cat a.sh #!/bin/sh ... (3 Replies)
Discussion started by: prejib
3 Replies

6. Shell Programming and Scripting

Need to write a script in UNIX to find a file if another file exists

So I have a lot of Java applications on my servers all having their own folder from the applications subdirectory. Now, I need to do the following. Search all the applications subdirectories for message.jar. If the message.jar file exists, I need to search the application directory for... (1 Reply)
Discussion started by: mmdawg
1 Replies

7. Shell Programming and Scripting

How to write a script by using unix commands to down any server

Hi, I need to do frequently server down and up. Every time i am using nearly 5 to 6 commands to down and agin i am using the commands to up. Can anybody help me to write a script to down and up. which i can use once on unix platform it can down later it can up the server. (1 Reply)
Discussion started by: sreerao
1 Replies

8. UNIX for Dummies Questions & Answers

How to write a script by fork() in unix

Hello to UNIX Champs, Can any body help me out to write the script using fork() thru shell scripting.....i am a layman to fork(), so please give me the link or any scripts which will help me out to know the details about fork. (1 Reply)
Discussion started by: manas_ranjan
1 Replies

9. UNIX for Dummies Questions & Answers

I'm not familiar with UNIX at all....HELP!

I need to know how to update our UNIX systems for 2007 DST. Problem is, I have no idea what version of UNIX we have or how to even find out what version it is. I work in the IT department and none of my co-workers know anything about UNIX either. So, I have two questions. First of all, how do I... (5 Replies)
Discussion started by: mcrawford575
5 Replies

10. Shell Programming and Scripting

how to write examination(multiple choice) script in unix

Hi, I am unable to get idea, how to write the script like multiple choice exam model script , how to specify the welcome to the exam(name) and if he select option then it has to enter into the exam , then choosing the answers,next question and going to previous question. Thanks & Regards ... (0 Replies)
Discussion started by: palreddy7
0 Replies
Login or Register to Ask a Question