LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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-14-2012, 03:19 PM   #1
mrjawz
LQ Newbie
 
Registered: Feb 2012
Location: Washington D.C.
Posts: 2

Rep: Reputation: Disabled
Adding file extensions to listings in a txt file


Good evening Linux Gods,
I have a text file full of linux pkgs. The pkgs were copied from an install directory I created for our kickstart server. it contains the pkg info need to baeline our build. The problem I want to be able to add the ".rpm" to all of the files listed without doing so by hand. I have been unsuccessful in my scripting attempts. File not found is the most common error, Im receiving.

My feeble attempts:
for i in `grep Installing installing.log.file | awk '{print $2}'`
do echo $i >> filename.

The filename file has the files listed, but no .rpm extensions.

Help
 
Old 02-14-2012, 03:59 PM   #2
nix422
LQ Newbie
 
Registered: Feb 2012
Posts: 1

Rep: Reputation: Disabled
dont really get your question, but why not just add a .rpm to your echo command...?
Code:
 i=5; echo $i.rpm
 
Old 02-14-2012, 04:20 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Yeah... I'm with nix422.

I don't follow... At least, I'm not confident that I know what you're trying.

From what you posted:
Code:
for i in `grep Installing installing.log.file | awk '{print $2}'`
do echo $i >> filename
Ok, so you're trying to loop line-by-line through a grep-awk pipeline.

The grep is supposed to report only lines that include 'Installing" from the file named "installing.log.file". The awk removes all but the second column from each matching line--presumably leaving the package name.

The next line simply appends the package name verbatim to the end of a file named "filename".

There are some better ways of doing this kind of thing. Personally, I don't like trying to shove a whole big bunch of names into a long "for x in some_really_long_list" but prefer loops. For instance:
Code:
#!/bin/bash

grep Installing installing.log.file | \
awk '{print $2}' | \
while read packageName ; do
  echo ${packageName}.rpm >> filename # as demonstrated by nix422
done
Quote:
Originally Posted by mrjawz
File not found is the most common error, Im receiving.
Nothing in your post suggests a "file not found" error unless installing.log.file is the wrong filename or it is saved in a different directory--which would require a full path to reference the file.

EDIT:
Forgot to mention--the "better way" of doing such things. This can all be rolled into a single sed command.
Code:
sed -rn '/\bInstalling\b/ s@[^[:space:]]+[[:space:]]+([^[:space:]]+).*@\1.rpm@p' installing.log.file
You can then redirect the output of the command to filename like you did earlier.

You could probably do something similar with awk, but I'm not that familiar with it.

Last edited by Dark_Helmet; 02-14-2012 at 04:43 PM.
 
Old 02-14-2012, 11:23 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
I would have said that a simple sed would be the easiest solution. Perhaps if you showed the file content (just a portion) we could best tailor a solution for you?
 
Old 02-15-2012, 12:41 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
Well, if its straight 1 file per line list text file like
Code:
qwe
asd
zxc
then
Code:
:$s/$/.rpm/
in vi works for me
 
Old 02-15-2012, 09:53 AM   #6
mrjawz
LQ Newbie
 
Registered: Feb 2012
Location: Washington D.C.
Posts: 2

Original Poster
Rep: Reputation: Disabled
First of all thank you for your assistance,
The question is very rush and unclear, my fault. Im new to this team and Im trying to automate as much as I can. My core problem was editing a text file filled with filenames. I wanted to add the extension .rpm to the filename (linux pkgs). I had a brain fart and over complicated the issue. When I saw nix422, i realized my thinking was off.

Thank you again, grail, nixx, chris, dark helmet for your responses.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
cut first 10 lines of file master.txt and paste in ab1.txt and so on yogeshkumkar Programming 4 08-31-2011 08:23 AM
Copy the contents of a txt file to other txt files (with similar names) by cp command TheIndependentAquarius Linux - Newbie 7 07-03-2010 01:54 AM
Searching .txt file for (specific) strings and printing them to new file Hb_Kai Linux - General 7 02-18-2010 10:09 AM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 06:11 PM
How to convert a txt file to be a db file in Redhat linux 9? winnie Linux - Newbie 3 06-27-2003 09:33 AM


All times are GMT -5. The time now is 07:09 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration