Need help in creating a file in required format form another existing file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in creating a file in required format form another existing file
# 1  
Old 04-28-2015
Need help in creating a file in required format form another existing file

I have a text file with contents like this:
Code:
a,b,c,
d~e,f,g,h~i,j
,k,l,m~n,o,p,q~

I need to convert this file into this format unix shell script commands:
Code:
a,b,c,d~
e,f,g,h~
i,j,k,l,m~
n,o,p,q~

as you may have noticed, I need to retain the ~ signs at the end.

Any help is greatly appreciated.

Last edited by Franklin52; 04-29-2015 at 07:10 AM.. Reason: Please use code tags
# 2  
Old 04-28-2015
Any attempt from your side?

---------- Post updated at 21:20 ---------- Previous update was at 21:13 ----------

Anyhow, try
Code:
awk '{X=X $0} END{gsub(/~/,"&\n",X); printf "%s", X}' file
a,b,c,d~
e,f,g,h~
i,j,k,l,m~
n,o,p,q~

This User Gave Thanks to RudiC For This Post:
# 3  
Old 04-28-2015
I tried these steps:
  1. Used the below command to roll all the lines into a single line
Code:
sed -i -e :a -e 'N;s/\n//;ta' input_file.txt

2. Then converted the ~ to a new line


Code:
sed -i 's/~/~\n/g' input_file.txt

This approach is working, but I am concerned about the record size after the first step - if my initial filw size is huge, then would I run into a probelm if I roll up all the lines into a single line?
# 4  
Old 04-28-2015
Or, for long lines:
Code:
awk '/^\n$/ {next} {gsub(/\n/,""); $0=$0"~"} 1' RS="~"  file

---------- Post updated at 21:47 ---------- Previous update was at 21:44 ----------

or
Code:
awk '{gsub(/\n/,"")} $0 {print $0 RS}' RS="~"  file

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Creating the script for updating or replacing the existing http.conf file

Hi I need some help with a task, i am an absolute newbie to any form of shell scripting and request guidance. I have been building a proxy server using the apache mod proxy currently my solution is working , but i need to automate the process , suppose if any changes need to be made on... (0 Replies)
Discussion started by: satej
0 Replies

2. UNIX for Dummies Questions & Answers

help required in converting a file format

My file format: -------------------------------------------------- Complete Consistency Check Valid Area : VALID:VALID Started by : esanwad Started at : Thu Dec 11 16:04:46 2014 CNA version : R21H04_EC08 Check range : AREA VALID/VALID ... (4 Replies)
Discussion started by: Gautam Banerjee
4 Replies

3. Shell Programming and Scripting

Need help to format one txt file to required format

Hello Everyone, I have one source file which is genarated by SAP in different format(Which I've never seen). I need to convert that file to required format and I need to read this target file from Datastage to use this in my Jobs. So I do not have any other options except to use Unix script to... (4 Replies)
Discussion started by: Prathyu
4 Replies

4. Shell Programming and Scripting

Creating file from an existing file using CUT, is it the best option?

Dear All, I have a requirement in which i have to load a file placed in FTP location onto my database. The process i'll follow is as below: 1) Get the files using FTP. 2) Create the desired load files as i have to load only 19 fields out of the 104 available in the file. The fields i require... (7 Replies)
Discussion started by: abhishekakaomi
7 Replies

5. Shell Programming and Scripting

Creating a new file based on existing file

Hello Guys , I need an another help regarding the below problem. I want to create a new file based on the existing file ,where two columns will be changed according to user input .(say column 4 and column 5) Please let me know how to proceed with Thanks (3 Replies)
Discussion started by: Pratik4891
3 Replies

6. Shell Programming and Scripting

Creating/ammending Name Column in existing .txt file

With the help of this forum, I have a script with the following output: chr7 27104483 27105154 chr7 27106872 27110789 chr7 27111956 27112830 chr7 27114388 27125180 chr7 27126966 27131260 chr7 27135440 27137796 which was created by the following script: awk '1 == NR || $NF >= 1000 {... (6 Replies)
Discussion started by: awknerd
6 Replies

7. Shell Programming and Scripting

Creating a csv file based on Existing file

Hi I am Newbie to Unix.Appreciate Help from forum user would loada b.Csv File(Below example) in /data/m/ directory.Program need to read the b.csc to extract certain column and create a new file /data/d/ directory as csv file with new name. User File Format 1232,samshouston,12345... (3 Replies)
Discussion started by: skywayterrace
3 Replies
Login or Register to Ask a Question