LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   I need to parse a text file in shell scripting (https://www.linuxquestions.org/questions/linux-newbie-8/i-need-to-parse-a-text-file-in-shell-scripting-907786/)

shanti sree 10-12-2011 09:46 AM

I need to parse a text file in shell scripting
 
---

crts 10-12-2011 10:02 AM

Quote:

Originally Posted by shanti sree (Post 4496579)
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.

colucix 10-12-2011 10:05 AM

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


grail 10-12-2011 10:43 AM

So did I miss something? Why has the OP pulled the question??

colucix 10-12-2011 02:09 PM

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.


All times are GMT -5. The time now is 01:48 AM.