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-14-2012, 02: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, 02: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, 03:20 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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 03:43 PM.
 
Old 02-14-2012, 10:23 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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-14-2012, 11:41 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 08: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



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 07:23 AM
Copy the contents of a txt file to other txt files (with similar names) by cp command Aquarius_Girl Linux - Newbie 7 07-03-2010 12:54 AM
Searching .txt file for (specific) strings and printing them to new file Hb_Kai Linux - General 7 02-18-2010 09: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 05:11 PM
How to convert a txt file to be a db file in Redhat linux 9? winnie Linux - Newbie 3 06-27-2003 08:33 AM

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

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