LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-12-2011, 09:46 AM   #1
shanti sree
LQ Newbie
 
Registered: Oct 2011
Posts: 1

Rep: Reputation: 0
I need to parse a text file in shell scripting


---

Last edited by shanti sree; 10-12-2011 at 10:32 AM.
 
Old 10-12-2011, 10:02 AM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by shanti sree View Post
I need to parse a text file in shell scripting.

Txt file format:-


Channel Name Service Handle Package Name BSG Handle Package_tier
FX East 48129 47100 Premier (EB) 47100 4168
FX West 48130 47100 Premier (EB) 47100 4168
Resource Authorization in Effe 259 47100 Premier (EB) 47100 4168



This file is having 5 columns and the below once are values and i need to get output as

The columns names are
Channel Name ,Service Handle ,Package Name, BSG Handle,Package_tier
And others are all values.

output as:-



Channel Name : FX East ; Service Handle : 48129 ; Package Name : 47100 Premier (EB) ; BSG Handle : 47100 ; Package_tier : 4168
Channel Name : FX West ; Service Handle : 48130 ; Package Name : 47100 Premier (EB) ; BSG Handle : 47100 ; Package_tier : 4168


Can anyone please help me.
Hi,

whenever you post sample data or code you should use code-tags. The data you provided looks quite messy. Are the columns really separated by space or TAB? It is easy to achieve what you want if the data is TAB separated. Not so easy if it is in fact space separated. Please clarify on that and post some representative sample data.
 
Old 10-12-2011, 10:05 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Assuming the fields are TAB separated...

Awk:
Code:
awk -F"\t" 'NR > 1 {print "Channel Name :", $1, "; Service Handle :", $2, "; Package Name :", $3, "; BSG Handle :", $4, "; Package_tier :", $5}' file
Bash:
Code:
#!/bin/bash

IFS=$(echo -e "\t")
skip=1

while read a b c d e
do
  if [[ $skip == 0 ]]
  then
    echo "Channel Name : $a ; Service Handle : $b ; Package Name : $c ; BSG Handle : $d ; Package_tier : $e"
  fi
  skip=0
done < file
 
Old 10-12-2011, 10:43 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So did I miss something? Why has the OP pulled the question??
 
Old 10-12-2011, 02:09 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Mah! I have seen many situations like this and still I cannot understand the reasons for such a rude behavior. If the OP returns it's ok, otherwise this thread is eligible for closure. Nuff' said.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed - parse text file elexx Programming 8 02-22-2011 10:19 AM
Shell Scripting text file creation issue AiresTheBold Linux - Newbie 1 02-16-2011 09:27 AM
writing data or text to a file using shell scripting vinaytp Linux - Newbie 3 04-24-2009 12:16 AM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM
Reading text from a file using shell scripting. mrobertson Programming 11 06-29-2005 12:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration