[Solved] Formatting the text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Formatting the text file
# 1  
Old 06-21-2013
[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:-

Code:
 
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 10.240.8.231 N 
framps06.corp.go2uti.com 10.254.7.146 N 
SINMIY01.devtst-corp.go2uti.com 10.240.9.164 N

want to change it to
Code:
 
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           10.240.8.231                   N 
framps06.corp.go2uti.com             10.254.7.146                   N
SINMIY01.devtst-corp.go2uti.com      10.240.9.164                   N 
useipapd01                           172.22.32.87                   Y

# 2  
Old 06-21-2013
use translate..


Code:
[root@pavan ~]# cat test_script|tr " " "\t"
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      10.240.8.231    N
framps06.corp.go2uti.com        10.254.7.146    N
SINMIY01.devtst-corp.go2uti.com 10.240.9.164    N
[root@pavan ~]#

# 3  
Old 06-21-2013
something like this?

Code:
 
awk '{if(NR==1){print $1" "$2"\t\t\t\t"$3" "$4"            "$5}else{gsub(" ","\t\t",$0);print $0}}'  filename

# 4  
Old 06-21-2013
Thanks VidyaDhar,

I tried but it is not giving me output in the required format. It is giving me in the below format.

Code:
Host/Alias Name IP Address Resolved
sinuiy01.infra.go2uti.com 10.240.8.158 N N
sinuid20.devtst.go2uti.com 10.240.8.230 N N
sinuid21.devtst.go2uti.com 10.240.8.231 N N
framps06.corp.go2uti.com 10.254.7.146 N N
SINMIY01.devtst-corp.go2uti.com 10.240.9.164 N N
myuti.teams.go2uti.com 10.254.7.146 N N
sinmpm01.corp.go2uti.com 10.240.7.163 N N
rtc.go2uti.com 10.240.6.148 N

Moderator's Comments:
Mod Comment Tested: Vidayar's solution works, on AIX anyway...
Code:
n12:/home/vbe/test/awk $ awk '{if(NR==1){print $1" "$2"\t\t\t\t"$3" \
"$4"            "$5}else{gsub(" ","\t\t",$0);print $0}}'  filename
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              10.240.8.231            N
framps06.corp.go2uti.com                10.254.7.146            N
SINMIY01.devtst-corp.go2uti.com         10.240.9.164            N
n12:/home/vbe/test/awk $


Last edited by vbe; 06-21-2013 at 11:11 AM..
# 5  
Old 06-21-2013
Assumed your Host/Alias Name size is 50 char long
Code:
 
awk 'BEGIN{printf "Host/Alias Name\t\t\t\t\t\tIP Address\tResolved\n";}
NR>1{printf "%-50s\t%-15s\t%8s\n",$1,$2,$3}' filename

This User Gave Thanks to pravin27 For This Post:
# 6  
Old 06-21-2013
Hi.

Using utility align for automatic alignment:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate automatic field alignment with "align".
# See: http://freecode.com/projects/align

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C sed align

FILE=${1-data1}

pl " Input data file $FILE, fixed for embedded spaces:"
sed 's/IP A/IP_A/;s/s N/s_N/;s/  */\t/g' $FILE > f1
cat f1

pl " Results:"
align -s t+ f1

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
sed GNU sed version 4.1.5
align 1.7.0

-----
 Input data file data1, fixed for embedded spaces:
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	10.240.8.231	N	
framps06.corp.go2uti.com	10.254.7.146	N	
SINMIY01.devtst-corp.go2uti.com	10.240.9.164	N

-----
 Results:
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	10.240.8.231	N
framps06.corp.go2uti.com	10.254.7.146	N
SINMIY01.devtst-corp.go2uti.com	10.240.9.164	N

Restoration of spaces for underlines left as exercise.

Output on (my) terminal shows white space between all fields. The forum display may show some fields abutted without space, possibly due to handling of TABs.

See URL in comments in script for source of perl code align.

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 7  
Old 06-21-2013
Thanks Praveen\DRL both approaches are working for me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Formatting text file

Hi All, how to format text Thanks (19 Replies)
Discussion started by: ROCK_PLSQL
19 Replies

2. 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

3. Shell Programming and Scripting

[Solved] Need help formatting a file

I have a report similar to the below: ^L"0.1","Run Date : 19/11/10 Navneet Bank, N.A. PAGE NO : 1" "0.2",Proc Date : 19/11/10 GLOBAL A/C SYSTEM ... (2 Replies)
Discussion started by: Gangadhar Reddy
2 Replies

4. Shell Programming and Scripting

Solved: Text formatting

What's the best way to format the text file shown below the way in which COlA ColB ColC ColD ---- ---- ---- ---- 70 923 AAA 5864 70 testing AAA 13062 PPPPPP test AAA 142 (0 Replies)
Discussion started by: joe228
0 Replies

5. 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

6. 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

7. 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

8. 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

9. 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

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