Problem in using cut command with pipe as a delimiter while using in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem in using cut command with pipe as a delimiter while using in a script
# 1  
Old 06-03-2017
Display Problem in using cut command with pipe as a delimiter while using in a script

There is a text file in my project named as "mom.txt" in which i want to have contents like..................

Code:
LSCRM(Application Name):
1: This is my first application.
2: Today we did shell scripting automation for this app.
3: It was really a good fun in doing so.
4: Really good.| (Here i am using pipe)

MCRM(Another application name)
1: This is MCRM App
2: Secondary app.
3: Good app and so on.| (Using pipe)

AND THE SAME ABOVE MENTIONED FORMAT FOR REST APPLICATION.

Now I have another HTML file called "momcpy.html" which i create in the script mentioned below where i have different text fields/area with their respective name.
Now i want to cut values from mom.text file by using my script file when user inputs in the same format that i have mentioned above for LSCRM Application and replace the cut value in the HTML file in the specific text area assigned to that application(Let us assume the text area name for LSCRM App is "lscrm" and so on for rest application). But in my case, cut command isn't working properly when i am using pipe as delimiter. For LSCRM app, i want to cut values from point 1 to point 4 and replace this cut value in its text area and same for MCRM in its text area. You can see, i have used pipe as delimiter above. Plz help asap, appreciated in advance.

My script is :


Code:
#! /bin/bash -x
file='/home/websphe/tomcat/webapps/MOM/mom.txt'
file1='/home/websphe/tomcat/webapps/MOM/web/mom.html'
common_path='/home/websphe/tomcat/webapps/MOM/web/'
cp $file1 $common_path/momcpy.html
if test -s $file
   then
attendees=`cat $file | cut -d '|' -f2`
echo $attendees
lscrm=`cat $file | cut -d '|' -f3`
echo $lscrm
mcrm=`cat $file | cut -d '|' -f4`
echo $mcrm
tlgapi=`cat $file | cut -d '|' -f5`
echo $tlgapi
concerns=`cat $file | cut -d '|' -f6`
echo $concerns
appreciation=`cat $file | cut -d '|' -f7`
echo $appreciation
perl -p -i -e "s#attendees#$attendees#g" $common_path/momcpy.html
perl -p -i -e "s#lscrm#$lscrm#g" $common_path/momcpy.html
perl -p -i -e "#mcrm#$mcrm#g" $common_path/momcpy.html
perl -p -i -e "s#tlgapi#$tlgapi#g" $common_path/momcpy.html
perl -p -i -e "s#concerns#$concerns#g" $common_path/momcpy.html
perl -p -i -e "s#appreciation#$appreciation#g" $common_path/momcpy.html
     echo "`/bin/sh ./mail.sh`"
             rm $file
             rm $common_path/momcpy.html
  else
    echo "Sorry no email sent :("
fi
~


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 06-03-2017 at 04:58 PM.. Reason: Added CODE tags.
# 2  
Old 06-03-2017
Your script is set up to trace its activity while it is running. If you show us that trace output, we'll have a much better chance of understanding what you're talking about.

You have shown us that you are trying to extract data from seven vertical bar separated fields in your input file, but the sample input file you have shown us never has more than one vertical bar. So, fields 3 through 7 must always be empty.

About all I can say that could be done to solve your problem with what you have shown us would be to rewrite your script to extract data from you input file in a way that matches the format of the data you are processing.
# 3  
Old 06-04-2017
Hi Cragun,

First of all thankyou for your time ! Here i didn't understand what you told that 7 vertical bars. Its like suppose in my text file , i have data that looks like:

Code:
LSCRM(Application Name):
1: This is my first application.
2: Today we did shell scripting automation for this app.
3: It was really a good fun in doing so.
4: Really good.| (Here i am using pipe)

After Point 4 you can see pipe as delimiter. Now if i want to cut this whole lscrm value from text file and replace the cut value in a HTML file in text area by:

****************
Code:
lscrm=`cat $file | cut -d '|' -f1`
perl -p -i -e "s#lscrm#$lscrm#g" $common_path/momcpy.html

***************************
I didn't see this value........ "
Code:
1: This is my first application.
2: Today we did shell scripting automation for this app.
3: It was really a good fun in doing so.
4: Really good

" ...........that get cut.

Instead , i see only the first line that is point 1 got cut.i.e. "1: This is my first application."

So, plz reply with any command that can help me get the desired value.

Thanks


Moderator's Comments:
Mod Comment Seriously: Please use CODE tags as required by forum rules!

Last edited by RudiC; 06-04-2017 at 06:50 AM.. Reason: Added CODE tags.
# 4  
Old 06-04-2017
Please make sure you're using code tags as required in the future!


Almost all *nix text tools work on a per line basis, i.e. on an arbitrary number of characters terminated by a <LF> (\n, 0x0A) char. That's why cut yields the entire file except for any line's pipe char and rest of that line.
My perl is non-existent, but with awk you could get at your desired result like
Code:
awk  -F'|' '{print $1}' RS="" file
LSCRM(Application Name):
1: This is my first application.
2: Today we did shell scripting automation for this app.
3: It was really a good fun in doing so.
4: Really good.

Allow me to comment that your data structure as well as your chosen approach seem somewhat unorthodox and inefficient.

Last edited by RudiC; 06-04-2017 at 01:38 PM.. Reason: typo
# 5  
Old 06-04-2017
Quote:
Originally Posted by Abhijeet Anand
Hi Cragun,

First of all thankyou for your time ! Here i didn't understand what you told that 7 vertical bars. Its like suppose in my text file , i have data that looks like:

... ... ...

So, plz reply with any command that can help me get the desired value.

Thanks


Moderator's Comments:
Mod Comment Seriously: Please use CODE tags as required by forum rules!
The data that you have in your text file and the commands you are using to extract data from that text file do not seem well suited to what you are trying to do. Please create a file named test.txt that contains the following text:
Code:
f1|f2|f3|f4|f5|f6|f7
field 1|field 2|field 3|field 4|field 5|field 6|field 7
3: It was really a good fun in doing so.
4: Really good.| (Here i am using pipe)

After creating that file, what output do you get when you run the command:
Code:
cut -d '|' -f2 test.txt

What output do you get when you run the command:
Code:
cut -d '|' -s -f2 test.txt

What output do you get when you run the command:
Code:
cut -d '|' -f2,4-6 test.txt

After running these commands do you understand what I meant about the 6 vertical bars (AKA pipe symbols)? Do you see that the cut commands in your script are extracting data from 7 pipe symbol delimited fields from lines in the input you feed into it, but only two of those lines contain any pipe symbols?

Have you read the manual page for cut on your system? If not enter the command:
Code:
man cut

and look at how it describes the -d, -f, and -soptions. I know that you aren't using the -s option in your code, but (with the possible exception of when you are extracting field 1 data) you might want to consider it. Do you understand now what I meant when I said "you are trying to extract data from seven vertical bar separated fields in your input file"?

Last edited by Don Cragun; 06-04-2017 at 01:43 PM.. Reason: Fix typo: s/7/6/
# 6  
Old 06-04-2017
Hi RudiC,

As u mentioned about the awk command, i tried using it, still i didn't get the desired result.

On running :
Code:
awk -F'|' '{print $1}' RS="" input file

My output result is only LSCRM:
But i want LSCRM:
Code:
1: This is my first application.
2: Today we did shell scripting automation for this app. 
3: It was really a good fun in doing so. 
4: Really good

.

As this the value user is supposed to give as an input.Any help highly appreciated !

---------- Post updated at 02:03 PM ---------- Previous update was at 01:49 PM ----------

Hi Cragun,

I understood what you tried to explain me about 7 vertical bars.
And i also tried by creating test.txt file and ran the following commands.

But i have an input field as
Code:
LSCRM: 
1: This is my first application.
2: Today we did shell scripting automation for this app. 
3: It was really a good fun in doing so. 
4: Really good.

So, to cut All those 4 lines together and store it in a varaible, which command or which delimiter shall i use?

I dont want to have in f1|f2|f3|f4|f5|f6|f7 this format.

It should be
Code:
f1
1:
2:
3:
4:    | (Means f1 ends after line 4)
f2
1:
2:
3:   |( f2 ends after line 3)

Thanks

Last edited by Abhijeet Anand; 06-04-2017 at 03:56 PM..
# 7  
Old 06-04-2017
With an input file
Code:
cat file
LSCRM(Application Name):
1: This is my first application.
2: Today we did shell scripting automation for this app.
3: It was really a good fun in doing so.
4: Really good.| (Here i am using pipe)

MCRM(Another application name)
1: This is MCRM App
2: Secondary app.
3: Good app and so on.| (Using pipe)

LSCRM(Application Name):
1: This is my first application.
2: Today we did shell scripting automation for this app.
3: It was really a good fun in doing so.
4: Really good.| (Here i am using pipe)

,
Code:
awk -F'|' '{print $2}' RS="" file

delivers
Code:
 (Here i am using pipe)

MCRM(Another application name)
1: This is MCRM App
2: Secondary app.
3: Good app and so on.

, so I guess something is wrong with your file / setup. Please give us way more context / background.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut command with dynamic passing of delimiter and position values

Hi All, We have a requirement of picking nth position value by using cut command. value would be delimited by any symbols. We have to pass delimited value and postition to get the value in a string. ex. echo "A,B,C,D,E" |cut -d "," -f3 echo "A|B|C|D|E"|cut -d "|" -f2 Kindly frame the... (5 Replies)
Discussion started by: KK230689
5 Replies

2. Shell Programming and Scripting

How to cut by delimiter, and delimiter can be anything except numbers?

Hi all, I have a number of strings like below: //mnt/autocor/43°13'(33")W/ and i'm trying to get the numbers in this string, for example 431333 please help thanks ahead (14 Replies)
Discussion started by: sunnydanniel
14 Replies

3. UNIX for Dummies Questions & Answers

set output delimiter as tab in cut command

I can not make it work, it prints \t rather than introduce tabs. cut -d "," -f 4,8 Samples.csv --output-delimiter="\t" | sort > out Since I am running this command within a shell script, I tried manually inserting tab in this command, still does not work. I am using bash shell Suggestions... (8 Replies)
Discussion started by: analyst
8 Replies

4. Shell Programming and Scripting

CUT command delimiter in reverse

Hi, I've a situation where, a=xxx.yyy.zzz.txt EXTN=`echo $a | cut -d . -f2` Using the above code it delimites and will return "yyy.zzz.txt" to EXTN. But i need to get only the extension "txt". so as per the above code it delimits in the first "." itself. Can anyone help how to do... (6 Replies)
Discussion started by: skcvasanth
6 Replies

5. Shell Programming and Scripting

Problem using cut command in shell script

I'm new to shell programming, and am having a problem in a (Korn) shell program, which boils down to this: The program reads a record from an input file and then uses a series of "cut" commands to break the record into parts and assign the parts to variables. There are no delimiters in the... (2 Replies)
Discussion started by: joroca
2 Replies

6. UNIX for Dummies Questions & Answers

Problem Using Cut With A Space Delimiter

I am trying to extract 'postmaster' from the following string: PenaltyError:=554 5.7.1 Error, send your mail to postmaster@LOCALDOMAIN using the following command: cat /usr/share/assp/assp.cfg | grep ^PenaltyError:= | cut -d '@' -f1 | cut -f8 but it returns: PenaltyError:=554 5.7.1 Error,... (10 Replies)
Discussion started by: cleanden
10 Replies

7. UNIX for Dummies Questions & Answers

replacing space with pipe(delimiter)

Hello All, I have a file with thousands of records: eg: |000222|123456987|||||||AARONSON| JOHN P|||PRIMARY |P |000111|567894521|||||||ATHENS| WILLIAM k|||AAAA|L Expected: |000222|123456987|||||||AARONSON| JOHN |P|||PRIMARY |P |000111|567894521|||||||ATHENS| WILLIAM |k|||AAAA|L I... (6 Replies)
Discussion started by: OSD
6 Replies

8. UNIX for Dummies Questions & Answers

Encoding Problem while using "|" (PIPE) as delimiter from Mainframe to Unix

We are facing a problem with PIPE (|) as a delimiter in one of our FTP flat files. We are constructing a Flat file in IBM-AIX and this contains various strings delimted by PIPE Symbol and then FTPing this to a Mainframe System The Mainframe program simply recieves this and FTPs the same... (1 Reply)
Discussion started by: seshendra
1 Replies

9. UNIX for Advanced & Expert Users

How can I use double character delimiter in the cut command

Hi All, Can the cut command have double character delimiter? If yes, how can we use it. if my data file contains : apple || mango || grapes i used cut -f1 -d"||" filename but got an error. Plz help.... Thanks. (1 Reply)
Discussion started by: AshishK
1 Replies

10. Shell Programming and Scripting

Dynamic delimiter in cut command

hello.. my prob is as follows: i have to read from a file which may have different formats depending upon the delimiter used in the data of the file.now i need that the user input the delimiter.the input delimiter is stored in a variable and is used on cut command to retrieve necessary... (3 Replies)
Discussion started by: tej.buch
3 Replies
Login or Register to Ask a Question