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 12-06-2015, 07:41 PM   #1
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,198

Rep: Reputation: 47
Bash help needed for creating lists


I have a file (ori_file) with following contents:

Code:
a_name1
a_name2
a_name3
I have to create 2 files.

First file (outfile1) should have following contents ( "abc" first, then original word, then this word with first character removed):

Code:
abc a_name1  _name1
abc a_name2  _name2
abc a_name3  _name3
Followed by another file (outfile2) with:

Code:
def _name1
def _name2
def _name3
How can this be done with a bash script? Thanks for your help.

Last edited by rng; 12-06-2015 at 07:42 PM.
 
Old 12-06-2015, 07:53 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
After more than 900 posts you should know how to post a question like this, and give what you have already tried etc, put some effort into it
 
1 members found this post helpful.
Old 12-06-2015, 09:44 PM   #3
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,198

Original Poster
Rep: Reputation: 47
I can put additional words using awk but cannot remove first character of the word:

processor.sh :
Code:
#! /bin/bash -f
awk '
BEGIN 	{} 
{print "abc\t",$1}	
END   	{} 
'
then:

Code:
$ cat ori_file | ./processor.sh
abc 	 a_name1
abc 	 a_name2
abc 	 a_name3

Last edited by rng; 12-06-2015 at 09:52 PM.
 
Old 12-06-2015, 11:43 PM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
I believe the awk function substr() would be a good approach. E.g. substr("a-name",2) yields "-name". For details, see the awk guide - the URL is in my signature.

The shell also has a way to generate a substring (I have the feeling that bash incorporates more and more awk-like text processing as time goes by). I don't know it by heart; check the variable substitution section in the bash reference manual http://www.gnu.org/software/bash/manual/bash.html.
 
Old 12-07-2015, 02:32 AM   #5
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,198

Original Poster
Rep: Reputation: 47
awk substr() function solved the issue. Thanks.
 
  


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
Creating Lists arnold.pietersen Programming 10 10-06-2014 02:13 PM
Bash script needed for creating csv matrix out of separate txt files ligtvoet Other *NIX 2 10-14-2011 06:24 PM
LXer: Unique Sorting Of Lists And Lists Of Lists With Perl For Linux Or Unix LXer Syndicated Linux News 0 09-05-2008 01:50 PM
auto creating 3 lists using filetypes mrgreaper Linux - Newbie 18 06-23-2006 04:24 AM
Creating Linked Lists from items in a file SeniorSE Programming 5 09-14-2005 10:53 PM

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

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