sed with listing


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers sed with listing
# 1  
Old 06-05-2018
sed with listing

Hi,

I would like to get latest file with using listing tail and want to replace header's first column starting from # to nothing.

I am using this to replace first header #

code :-
Code:
sed '1s/#//'

but I want to do something like this.

Code :-
Code:
ls promo_random.csv | tail -1 |  sed '1s/#//'

Output :-
Code:
promo_random.csv

Output should be the replacement of # of last file from listing.

Last edited by rbatte1; 06-05-2018 at 09:15 AM.. Reason: Added CODE tags
# 2  
Old 06-05-2018
Do you mean that you want to read the file's content rather than just the name?

Perhaps this might help:
Code:
sed '1s/#//' promo_random.csv

Does that help, or have I missed the point? It would help if you can explain a little more, preferably with the output you are getting, sample data from your file and the desired output. Please wrap all code, files, input & output/errors in CODE tags. Highlight the text required and press the button on the toolbar which has co over de. You can check and adjust it by pressing the Go Advanced button below.




Kind regards,
Robin
# 3  
Old 06-05-2018
Suppose, I have 2 files in directory
Code:
ls promo_random*.csv

Result:-
Code:
promo_random_20180401.csv
promo_random_20180501.csv

I want to pick last file which is latest and use as an input for sed command to replace# in that file

Last edited by Don Cragun; 07-01-2018 at 10:43 PM.. Reason: Add missing CODE tags again.
# 4  
Old 06-05-2018
Okay, that is possible. I'm assuming that the filename contains the date & time in a sorted format, do these are 1st April and 1st May. Does this help:-
Code:
sed '1s/#//' $(ls promo_random_*.csv | tail -1)

An alternate might be:-
Code:
for testfile in promo_random_*.csv
do
   sedfile="${testfile}"                                  # Remember this filename
done

[ ! -z "${sedfile} ] && sed '1s/#//' "${sedfile}"         # If the value of sedfile is not null, process it

You can argue that the first one spawns another few processes to run the ls & the tail so might be slower, but it's obviously more compact. I suppose it depends on the size of the list. It is probably negligible in most cases. If you are going to reprocess a large list, then it would probably be better to store it rather than to keep re-reading it.


Does that help?


Do you want the output to the screen (STDOUT) another file, or to update the file?


Robin
# 5  
Old 06-06-2018
Quote:
Originally Posted by rbatte1
Okay, that is possible. I'm assuming that the filename contains the date & time in a sorted format, do these are 1st April and 1st May. Does this help:-
Code:
sed '1s/#//' $(ls promo_random_*.csv | tail -1)

ls has a reverse option, of course, allowing one to ues head:
Code:
sed '1s/#//' $(ls -r promo_random_*.csv | head -1)

This may be quicker if you have a large number of files.

Andrew
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Help with listing only subfolders

Hi All, I have a AIX file system with a folder structure like this /1 /1/2 /1/2/3/5 /1/2/3/2 /2 /2/3/4 /2/5/6 I would like to list subdirectories /1/2/3/5 /1/2/3/2 /2/3/4 /2/5/6 Can you please help me with this issue. (6 Replies)
Discussion started by: kavinmjr
6 Replies

2. UNIX for Dummies Questions & Answers

[Solved] How to remove listing of current user cmd from ps -ef listing?

Hi All, Could you please help to resolve my following issues: Problem Description: Suppose my user name is "MI90". i.e. $USER = MI90 when i run below command, i get all the processes running on the system containing name MQ. ps -ef | grep MQ But sometimes it lists... (8 Replies)
Discussion started by: KDMishra
8 Replies

3. Shell Programming and Scripting

modify ls -l (long listing format output) strictly using SED only straightforward goalhard 4 me doh

Below is a sample out of ls -l which I would like to rearrange or modify by field numbers for example I successfully managed to disect using simple paragraph however for ls -l I can't divide the rows or fields by field number. Successful modification by fields using SED sample: $ sed -e... (1 Reply)
Discussion started by: wolf@=NK
1 Replies

4. UNIX for Dummies Questions & Answers

listing files

how would i list all files that began witha "t" and have any number of characters after the "t"? (2 Replies)
Discussion started by: trob
2 Replies

5. UNIX for Dummies Questions & Answers

Listing files

how would i list all files, directories and exectable files in my directory? is there anyway to find out what date a file was created? Thanks!! (2 Replies)
Discussion started by: trob
2 Replies

6. UNIX for Dummies Questions & Answers

Listing files

Hi there! I would like to list all the files from a directory and its subdirectories. For example: DIRECTORY |- SUBDIR1 ||- sub1.txt ||- sub2.txt |- SUBDIR2 ||- sub3.txt |- root1.txt |- root2.txt I would like to create a fulllist.txt file which contains the list of these files (with... (2 Replies)
Discussion started by: bobix
2 Replies

7. Shell Programming and Scripting

listing directories alone

ls lists all files and sub directories in the current directory but how to list only the sub directories and not the files? (2 Replies)
Discussion started by: bbala
2 Replies

8. Linux

Listing of files

How can I list all files in a directory and its subdirectories that have been created or changed since the system was booted. I was trying to acomplish this with "ls" and "find" commands but could not get anything usefull. Maybe some one can provide me a hint. Thank you for your time. (1 Reply)
Discussion started by: Vitalka
1 Replies

9. UNIX for Dummies Questions & Answers

Listing directories and ${1:+$1/}*

Hi I have 2 questions: Q1 - What does ${1:+$1/}* mean? I guess it lists all files in current directory - Could any one explain how this expression works? Q2 - I am trying to list directories only in current path - I know that ls could be used but I thought I'd give find a try. I need to... (5 Replies)
Discussion started by: GMMike
5 Replies

10. UNIX for Dummies Questions & Answers

Recursive directory listing without listing files

Does any one know how to get a recursive directory listing in long format (showing owner, group, permission etc) without listing the files contained in the directories. The following command also shows the files but I only want to see the directories. ls -lrtR * (4 Replies)
Discussion started by: psingh
4 Replies
Login or Register to Ask a Question