Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

shell script question

Thread Tools
 
Search this Thread
 
Old 19 March 2010, 12:30 PM
  #1  
P1_BEN
Scooby Regular
Thread Starter
 
P1_BEN's Avatar
 
Join Date: Apr 2002
Location: Kent
Posts: 460
Likes: 0
Received 0 Likes on 0 Posts
Question shell script question

guys need some help with a simple shell script creation

I have a flat text file which has several thousand lines which I need to convert to a csv it's got a consistent format but basically want every time it hit's txt to create a new line with

txt.1,line 1, line 2, line n
txt.2,line 1, line 2, line n
etc

The input file looks like this

txt.1
line 1
line 2
line n
txt.2
line 1
line 2
line n
txt.n
line n

Appreciate any help guys
Old 19 March 2010, 08:04 PM
  #2  
jpor
Scooby Regular
iTrader: (1)
 
jpor's Avatar
 
Join Date: Sep 2003
Posts: 3,109
Likes: 0
Received 0 Likes on 0 Posts
Default

Originally Posted by P1_BEN
guys need some help with a simple shell script creation

I have a flat text file which has several thousand lines which I need to convert to a csv it's got a consistent format but basically want every time it hit's txt to create a new line with

txt.1,line 1, line 2, line n
txt.2,line 1, line 2, line n
etc

The input file looks like this

txt.1
line 1
line 2
line n
txt.2
line 1
line 2
line n
txt.n
line n

Appreciate any help guys

You haven't given the shell environment and UNIX flavour you are using but here are a couple of things you can try, in the examples the file is called 'flatfile'

sed -n 'H;${x;s/\n/,/g;s/^,//;s/,$//;p;}' flatfile

or

xargs < flatfile | tr ' ' ','

Have tried these on AIX 5.3 TL9 SP4 using the Korn Shell Environment.
Should also in theory work in LINUX in BASH

Last edited by jpor; 19 March 2010 at 08:06 PM.
Old 26 March 2010, 02:41 PM
  #3  
warrenm2
Scooby Regular
 
warrenm2's Avatar
 
Join Date: Aug 2003
Location: Epsom
Posts: 5,832
Likes: 0
Received 0 Likes on 0 Posts
Default

or you could load it into word, and record a macro to do it....
Old 26 March 2010, 07:12 PM
  #4  
jpor
Scooby Regular
iTrader: (1)
 
jpor's Avatar
 
Join Date: Sep 2003
Posts: 3,109
Likes: 0
Received 0 Likes on 0 Posts
Default

Originally Posted by warrenm2
or you could load it into word, and record a macro to do it....
You could do that as a one off....but what happens if he starts to get these on a regular basis, I don't think he would want to ftp/sftp the output everytime into word or excel and then back again

Had to do something similar a few years back when a company used to send us output from one database we didn't have to which we had to translate and input into another. The only way to do this was to use a scheduled shell script and then upload this using an integrated SQL script. It looks like he is doing something similar.
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
KAS35RSTI
Subaru
27
04 November 2021 07:12 PM
Abx
Subaru
22
09 January 2016 05:42 PM
Mattybr5@MB Developments
Full Cars Breaking For Spares
28
28 December 2015 11:07 PM
Mattybr5@MB Developments
Full Cars Breaking For Spares
12
18 November 2015 07:03 AM
Brzoza
Engine Management and ECU Remapping
1
02 October 2015 05:26 PM



Quick Reply: shell script question



All times are GMT +1. The time now is 06:29 AM.