Formatting text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Formatting text file
# 8  
Old 07-21-2016
In perl, something similar to awk

Code:
$ perl -lne '$,=", "; @p=split(m/pname (.+?)[.].*=(.*),.*=(.*),.*: ?(.*) .*/,$_); print @p[1..4]' file
tmgnsr, 8, 5900, 27.682
tmgnsr, 5, 1234, 5.12
tmgnsr, 4, 878172, 27.145
tmgnsr, 4, 200, 34.624
tmgnsr, 4, 200, 8.076
tmgnsr, 3, 900, 37.393
tmgnsr, 1, 900, 43.077
tmgnsr, 8, 5900, 16.371
tmgnsr, 1, 30, 10.967
tmgnsr, 8, 900, 6.688
tmgnsr, 8, 900, 22.231

# 9  
Old 07-21-2016
Quote:
Originally Posted by ROCK_PLSQL
Hi ,
I have tried below one.I got the error ksh: syntax error: `=~' unexpected
Thanks
Well, as I said, my code is for zsh, so it is no surprise that it doesn't work with ksh.

I don't know the larger context of your script. Maybe it makes sense to convert the script to zsh, maybe it is better to use a different language.
# 10  
Old 07-21-2016
Moderator's Comments:
Mod Comment Hello ROCK_PLSQL,
Editing the first post in a thread to completely hide the reason for the other posts in the thread after you get your answer is totally inappropriate and, if it is done again in the future may be grounds for being banned from the UNIX & Linux Forums!

Please restore the content of the first message in this thread so that other users reading this thread can understand what this thread is discussing and learn from the suggestions made here to help you solve your problem.
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 07-22-2016
Hi,

In second field after the "." i want the date till the space.

I want the output as below

Code:
tmgnsr ,tpfile_01 , 8, 5900, 27.682
tmgnsr ,tpfile_01 , 5, 1234, 5.12
tmgnsr ,sdcfile_01 , 4, 878172, 27.145
tmgnsr ,sdcfile_02 , 4, 200, 34.624
tmgnsr ,sdcfile_03 , 4, 200, 8.076
tmgnsr ,mndfile_01 , 3, 900, 37.393
tmgnsr ,mndfile_02 , 1, 900, 43.077
tmgnsr ,mndfile_03 , 8, 5900, 16.371
tmgnsr ,mndfile_04 , 1, 30, 10.967
tmgnsr ,mndfile_05 , 8, 900, 6.688
tmgnsr ,mndfile_abc_01 , 8, 900, 22.231

I hav tried the below. But it's coming continuisouly , but it has to be sapareted by ","
Code:
tmgnsr.tpfile_01 , 8, 5900, 27.682
tmgnsr.tpfile_01 , 5, 1234, 5.12
tmgnsr.sdcfile_01, , 4, 878172, 27.145

Code:
awk -F"[ |=|: ]" '{sub(/\..*/,X,$1);print $2 OFS "," OFS $5 OFS $7 OFS $11}'   Input_file

Thanks
# 12  
Old 07-22-2016
@ROCK_PLSQL

Your post is not useful to anyone who follow this fora, see you removed your question in #1, what readers should think ? without question someone answered, person who answered here is mad ?
# 13  
Old 07-22-2016
Hi,

Sorry.By mistake I have removed.

Going forward I will take care of it.

Please help me.

Thanks

---------- Post updated at 05:03 PM ---------- Previous update was at 04:52 PM ----------

Hi RavinderSingh,

Can u please help me.

Thanks
# 14  
Old 07-22-2016
Quote:
Originally Posted by ROCK_PLSQL
Hi RavinderSingh,
Can u please help me.
Thanks
Hello ROCK_PLSQL,

I am not really sure how your Input_file looks as your previous post is not clear. Let's say following is your Input_file.
Code:
cat Input_file
tmgnsr ,tpfile_01 , 8, 5900, 27.682
tmgnsr ,tpfile_01 , 5, 1234, 5.12
tmgnsr ,sdcfile_01 , 4, 878172, 27.145
tmgnsr ,sdcfile_02 , 4, 200, 34.624
tmgnsr ,sdcfile_03 , 4, 200, 8.076
tmgnsr ,mndfile_01 , 3, 900, 37.393
tmgnsr ,mndfile_02 , 1, 900, 43.077
tmgnsr ,mndfile_03 , 8, 5900, 16.371
tmgnsr ,mndfile_04 , 1, 30, 10.967
tmgnsr ,mndfile_05 , 8, 900, 6.688
tmgnsr ,mndfile_abc_01 , 8, 900, 22.231

Then following is the code for same.
Code:
awk -F"," '{sub(/[[:space:]]+\,/,".",$0);print $0}'   Input_file

Output will be as follows.
Code:
tmgnsr.tpfile_01 , 8, 5900, 27.682
tmgnsr.tpfile_01 , 5, 1234, 5.12
tmgnsr.sdcfile_01 , 4, 878172, 27.145
tmgnsr.sdcfile_02 , 4, 200, 34.624
tmgnsr.sdcfile_03 , 4, 200, 8.076
tmgnsr.mndfile_01 , 3, 900, 37.393
tmgnsr.mndfile_02 , 1, 900, 43.077
tmgnsr.mndfile_03 , 8, 5900, 16.371
tmgnsr.mndfile_04 , 1, 30, 10.967
tmgnsr.mndfile_05 , 8, 900, 6.688
tmgnsr.mndfile_abc_01 , 8, 900, 22.231

If above doesn't satisfy your requirements then please post sample Input_file with expected sample output with all your conditions.

Thanks,
R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Formatting a text file

Hi All :), I have a formatting question and I am unsure on how I should proceed with my bash shell script. I am unsure weather to use perl or simply edit it in bash. I prefer bash but I am only aware of the awk utility to extract parts of a file, not edit output. Scenario: I have a file... (5 Replies)
Discussion started by: bstrizzy
5 Replies

2. Shell Programming and Scripting

[Solved] Formatting the text file

Hi All, I ahve requirement where I want to put the text file in into proper format. I am wondering how can i achieve that:- Host/Alias Name IP Address Resolved sinuiy01.infra.go2uti.com 10.240.8.158 N sinuid20.devtst.go2uti.com 10.240.8.230 N sinuid21.devtst.go2uti.com... (6 Replies)
Discussion started by: sharsour
6 Replies

3. Shell Programming and Scripting

Comparing and Formatting the text file

hi, I need a script which can format the below text file which contains comments file1.txt -------- //START //Name: some value //Date: //Changes:............. //..................... //END //START //Date: //Name: some value //Changes:............. //..................... (3 Replies)
Discussion started by: flamingo_l
3 Replies

4. Shell Programming and Scripting

Formatting the text file using shell script

How to add the filename to end of each line with | as seperator, except first and last line of the file(s) in directories(with diff tree structure) using shell script?. And also how to replace a list of strings with another set of strings, which is present in a file?. Kindly help out on... (1 Reply)
Discussion started by: av_vinay
1 Replies

5. Shell Programming and Scripting

Formatting text file in unix

Hi, I am using the following format command for formatting my text file in unix. awk -F":" '{ printf "%-50s%-1s%-50s\n", $1,":", $2}' filename > targetfile The target file is then sent as an attachment via email. When I view the target file in notepad multiple lines get spanned as a... (2 Replies)
Discussion started by: AAA
2 Replies

6. Shell Programming and Scripting

Formatting a text file to get data in exact line by line

I have my data something like this SERIAL FIRSTOCCURRENCE NETPROTOCOL 1947430693 07/01/2009 05:16:40 FR SERIAL FIRSTOCCURRENCE NETPROTOCOL 1947430746 07/01/2009 05:18:05 FR I want the output as follows.... (1 Reply)
Discussion started by: rdhanek
1 Replies

7. UNIX for Dummies Questions & Answers

formatting of the text file

Hi Guys, I have a file with contents in the below format DO_VJ_IDOC;03.23.2009;22:31:09; ZJDO_VJ_IDOC;03.23.2009;22:46:14; ZJDO_RESEND_FAILURES;03.24.2009;01:46:18; Now i need to replace the semicolons with tabs for which i am usig the sed command which gives the O/p as below ... (1 Reply)
Discussion started by: rohit.shetty84
1 Replies

8. UNIX for Dummies Questions & Answers

Text file formatting

Hi all! I'm new in unix, and faced with some difficulties. So I have text file f.e. "textfile" which contains rows like: aaa bbb ccc ddd How could I format it, so the file looks like: aaabbb cccddd Thanks in andvance (5 Replies)
Discussion started by: consta.v
5 Replies

9. Shell Programming and Scripting

Formatting a text file based on newline and delimiter characters

Hi Everybody, I need some help on formatting the files coming into unix box on the fly. I get a file some thing like this in a single line. ISA^M00^M ^M00^M ^M14^M006929681900 ^M01^M095449419 ... (5 Replies)
Discussion started by: ntekupal
5 Replies

10. Shell Programming and Scripting

text file formatting by perl

have a simple text file as input.i have to print that file in paragraph format.whenevr it finds "\n" in the input text it should start printing in next paragraph in output file.also a fixed amount of space should be given before start writing in every paragraph. the input and output file format... (5 Replies)
Discussion started by: avik1983
5 Replies
Login or Register to Ask a Question