LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-12-2013, 10:52 AM   #1
jeffwang66
LQ Newbie
 
Registered: Jan 2011
Posts: 27

Rep: Reputation: 0
A bash script


Hello,

I want to write a bash script to delete the content after '#'. However, if '#' appears in a string with "", ignore this. For example,

input file:

test #delete
"test #not delete"


Output file:

test
"test #not delete"

Does anyone know how to write this script?

Thanks
 
Old 11-12-2013, 11:57 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Your input example is rather small, but give this a try:
Code:
sed '/".*"/!s/#.*//' input
If the result is what you want/need you can change the command to make sure it is done in-place (inside the input file), this also makes a copy of the original:
Code:
sed -i.bak '/".*"/!s/#.*//' input
 
1 members found this post helpful.
Old 11-12-2013, 01:38 PM   #3
jeffwang66
LQ Newbie
 
Registered: Jan 2011
Posts: 27

Original Poster
Rep: Reputation: 0
Another example

Thanks for your help. But if the input file is:

input file:

test #delete
"test #not delete" #delete

I want the output is:

test
"test #not delete"
 
Old 11-12-2013, 02:14 PM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Code:
sed s/#[a-Z]*$// input

it works for your sample input
But will it work for your real data?
 
Old 11-12-2013, 03:37 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by jeffwang66 View Post
Thanks for your help. But if the input file is:

input file:

test #delete
"test #not delete" #delete

I want the output is:

test
"test #not delete"
Ok...so since you posted two identical threads, why haven't you also posted what YOU have done/tried so far??? We'll be glad to help you, but you have to show some effort of your own. Asking people to write scripts for you isn't a good thing.
 
Old 11-12-2013, 05:17 PM   #6
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
works here, Five by Five:
Code:
cat input && sed -i.bak '/".*"/!s/#.*//' input && cat input
test #delete
"test #not delete"
test 
"test #not delete"
Slackware-approved GNU sed version 4.2.1
 
Old 11-12-2013, 05:26 PM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by Habitual View Post
works here, Five by Five:
Code:
cat input && sed -i.bak '/".*"/!s/#.*//' input && cat input
test #delete
"test #not delete"
test 
"test #not delete"
Slackware-approved GNU sed version 4.2.1
but it doesn't work for the new criteria in post 3

input
Code:
test #delete
"test #not delete" #delete
this 'works'
Code:
sed s/#[a-Z]*$// input
but I guess, only by 'coincidence'
probably no good with 'real world' input data
 
Old 11-20-2013, 12:12 PM   #8
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
To cater for the stated requirements and also likely additional requirements:
Code:
cat input|sed 's/#[-=+a-zA-z0-9# ][-=+a-zA-z0-9#! ]*$//'|sed 's/#$//'
This runs two sed statements, the first to find # characters that are followed by one or more character in ranges a-z (lowercase alpha) A-Z (uppercase alpha) 0-9 (numeric) - a minus symbol = an equals symbol + a plus sign # a hash symbol a space character, where the second or later additional character can be ! an exclamation mark (removing the #!/bin/bash issues. The second sed statement removes a single hash at the end of a line.

There are many characters that could be added to the syntax, but this should meet most of the situations that I think you are trying to cater for.
 
Old 11-20-2013, 02:04 PM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Code:
sed -e 's/#[-=+a-zA-z0-9# ][-=+a-zA-z0-9#! ]*$//' -e 's/#$//' input
no need for cat, or pipe
 
Old 11-21-2013, 03:45 AM   #10
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
With this input:
Code:
$ cat file
test #delete
#delete "test #not delete" #delete "test again #not delete" test #delete me to the end
#delete
try
Code:
$ sed ':a;s/^\(\("[^"]*"\|[^"]*\)*\)#[^"]\+/\1/;ta;/^$/d' file
test 
"test #not delete" "test again #not delete" test
Hope this helps!
 
Old 11-22-2013, 04:24 AM   #11
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Wow!

I really must read the Sed & Awk book on my shelf!

I have only read a small section, and not for quite a while
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to end the bash script using commands in bash not manually by pressing ctrl+c Sanpreet Singh Linux - Newbie 1 07-03-2013 01:04 PM
[SOLVED] Converting Script from Linux (GNU) Bash 4 to Solaris Bash 2.05 - Any cheat sheet? oly_r Solaris / OpenSolaris 6 05-03-2013 08:25 AM
Why does this work from the bash command line and then fails in a bash script? Rupadhya Linux - Newbie 5 09-26-2012 12:05 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:40 PM.

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