LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-27-2012, 10:16 AM   #1
TigerClaw
LQ Newbie
 
Registered: Feb 2012
Posts: 2

Rep: Reputation: Disabled
Question [bash - sed - awk] Match line with x characters and add string


Hello,

I tried to find a way using the length() function of awk with no success.
I would like to add a string to the beginning or the end of a line that contains less than x character and more than Y characters

Example
Code:
Line:
This line contains less than 100 characters and more than 10
Result:
<string>This line contains less than 100 characters and more than 10
Or
This line contains less than 100 characters and more than 10</string>
Thanks!

Edit: inserted CODE tags

Last edited by TigerClaw; 02-27-2012 at 03:04 PM.
 
Old 02-27-2012, 10:27 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

It might help if you posted your non-working attempts too, so we can help point out where you're going wrong.

Awk can certainly do it, but I'm not going to bother with it right now. I will post a simple bash solution, however.

Code:
x=10
y=100

while read line; do

	ln=${#line}

	if (( ln > x && ln < y )); then
		echo "FOO $line"
	else
		echo "$line"
	fi

done <file.txt
 
Old 02-27-2012, 11:51 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Not sure how you could have got stuck??
Code:
awk 'length > 10 && length < 100{print $0,"<string>"}' file
And you can use -v to set variables if required.
 
Old 02-27-2012, 03:06 PM   #4
TigerClaw
LQ Newbie
 
Registered: Feb 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks both solutions works perfectly.

Sorry if my original question was not formulated in the right way, I'll try to do better next time.
 
Old 02-28-2012, 12:22 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please mark as SOLVED once you have a solution
 
  


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/awk to print every before line that pattern match niharikaananth Linux - Newbie 10 02-22-2012 10:47 PM
[SOLVED] BASH (grep / sed / awk): find string within a (line) range dragonetti Linux - Newbie 2 11-24-2011 10:16 PM
[SOLVED] Sed, Awk, Perl - Merge lines unless they match a certain string Dsw0002 Programming 5 04-16-2011 05:20 AM
[SOLVED] Need help in replacing set of characters in a specific line using sed or awk bbachu Programming 15 01-03-2011 01:01 AM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM

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

All times are GMT -5. The time now is 10:28 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