The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
grep unix.com with google



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-30-2009
Registered User
 

Join Date: Nov 2009
Posts: 5
How to change a file's content by row?

Greetings.
So the question is basically the same as it's in the title.
I'd like to write a program that changes a file by rows. So to clarify it.
(i know i shouldn't use code,/code here but i would like to separate it)
So for example a text file looks like something like this:

Code:
Happy Birthday!
Happy Mother's day!
Happy New Year!
.
.
.
Happy Blablabla!

and the result should look like:

Code:
Happy Blablabla!
.
.
.
Happy New Year!
Happy Mother's day!
Happy Birthday!

Sorry for this stupid example, but I think it is easier to understand in this way. The reason why i post this is that i've got no idea how to start it. I've searched for some commands that might switch the lines but found nothing.
Thanks for your help in advance and sorry for my english, might have spelled something wrong!
  #2 (permalink)  
Old 11-30-2009
dj - the student
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 667
Try tac command if you are on Linux.

or Try:


Code:
awk '!(A[i++]=$0); END { while(i--) print A[i] }'  file


Last edited by dennis.jacob; 11-30-2009 at 07:37 AM.. Reason: removed the unnecessary use of {}
Bits Awarded / Charged to dennis.jacob for this Post
Date User Comment Amount
11-30-2009 rdcwayx nice solution 100
  #3 (permalink)  
Old 11-30-2009
Registered User
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 1,258
If you are not in interested in using a command but in writing the script yourself, you can read all the lines into an array and then print the array from the highest index down to the lowest.
  #4 (permalink)  
Old 11-30-2009
Registered User
 

Join Date: Nov 2009
Posts: 5
I would like to write this as a script so i can use it whenever i will need it in the future. But i would like to change the file's structure. I mean i would like to overwrite the file.
  #5 (permalink)  
Old 11-30-2009
Registered User
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 1,258
You could write the result to a new file and then mv that new file to the old file.
  #6 (permalink)  
Old 11-30-2009
Registered User
 

Join Date: Jun 2006
Posts: 468
Quote:
Originally Posted by dennis.jacob View Post
Try tac command if you are on Linux.

or Try:


Code:
awk '!(A[i++]=$0); END { while(i--) print A[i] }'  file
Don't understand why put ! in awk,


Code:
awk '{A[i++]=$0} END { while(i--) print A[i] }' urfile

learn from dennis.jacob by using NR.


Code:
awk  '{A[NR]=$0} END {NR++; while (NR--) print A[NR]}' urfile

Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
change field content awk littleboyblu Shell Programming and Scripting 2 02-03-2009 11:57 PM
Change a file's timestamp shibajighosh AIX 6 05-27-2008 02:26 PM
how to change the content thru sed..... manas_ranjan Shell Programming and Scripting 3 09-22-2007 06:13 AM
Mass Change content in all files terala Shell Programming and Scripting 1 11-20-2005 01:30 AM
checking file's permissions and change them K-ONE Shell Programming and Scripting 7 04-10-2005 10:09 AM



All times are GMT -4. The time now is 01:22 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0