Simple sed command not working; could be a Mac/Linux vs. PC/Linux issue


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Simple sed command not working; could be a Mac/Linux vs. PC/Linux issue

Hello,

I am on a Mac and trying to clean up some monthly files with a very simple SED:

Code:
sed '3,10d;/<ACROSS>/,$d' input.txt > output.txt

(from the input, delete lines 3 - 10; then delete from the line containing <ACROSS> to the end of the file)
then output to output.txt

Even when I try just one of the above, that doesn't work, either.

Code:
sed '3,10d' input.txt > output.txt


To me, this looks like it should be pretty basic, but I am new to command line and am probably doing something wrong. Or, I'm told there are differences between Mac Linux and other PC Linux.

I'd rather it be me making the mistake, than different flavors of Linux.

Any help appreciated.

Moderator's Comments:
Mod Comment edit by bakunin: Please use CODE-tags for code, data and terminal output. Thank you.

Last edited by bakunin; 12-18-2018 at 08:17 AM..
# 2  
First things first: welcome to the forum. Please use CODE-tags when posting code, data or terminal output, like the ones i have edited into your post - it will make it stand out nicely and will prevent ambiguities caused by how HTML is handling white space too. Furthermore: please always state your complete problem! I take from the title and your posting that you are on MacOS and your sed script is not working - but HOW is it not working? Did you get some error message? Did the wrong output show up? Did no output show up? Something else? "Doesn't work" can have so many different meanings that i simply don't have enough data to properly analyse what is going wrong.

Quote:
Originally Posted by verbatim
Code:
sed '3,10d;/<ACROSS>/,$d' input.txt > output.txt

(from the input, delete lines 3 - 10; then delete from the line containing <ACROSS> to the end of the file)
then output to output.txt
A small correction: it means "delete every line which either has a line number 3-10 OR is in the range of a line containing "<ACROSS>" and the last line. Depending on the structure of your file that may make no difference or it may do so, i.e. if the line containing "<ACROSS>" is at the first line - then all lines (also line 1 and 2) would be deleted.

For this you need to understand how sed works: it reads in the input line after line and to every line one rule after the other is applied. You have two rules there: delete a line if its line number is 3-10 and delete a line if it is in the block from a line containing '<ACROSS>' and the last line. sed reads the first line, applies rule 1 to it - no, so don't delete. Then the second rule - maybe no, so still don't delete. Then it reads the next line, etc..


Quote:
Originally Posted by verbatim
Even when I try just one of the above, that doesn't work, either.
Code:
sed '3,10d' input.txt > output.txt

To me, this looks like it should be pretty basic, but I am new to command line and am probably doing something wrong. Or, I'm told there are differences between Mac Linux and other PC Linux.
This code looks correct and should do what you expect it to do. Still, to understand why it doesn't work i would need to know how it didn't work.

One misconception i already found: MacOS is NOT a Linux! You are partially right that MacOS is a UNIX with some graphical lumber on top of it. The underlying UNIX is in fact not a Linux but a FreeBSD, which is a different kind of UNIX-derivative. The difference on your problem is zero, though, so you still have a problem for some not understood reason, but i think it is worthwhile to clear these things up immediately and not after they "settled" into your thinking as they might hurt your understanding later.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
On top of what bakunin already said, it would be helpful to see a (or maybe even several) small sample input files with or without "ACROSS" in different positions, and the results (output / error msgs) of your scripts applied to them.
Where and how are your files generated? What are the line terminators?

Last edited by RudiC; 12-18-2018 at 12:13 PM..
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #357
Difficulty: Medium
BusyBox includes an AWK implementation written by Linus Torvalds.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to run simple single command on multiple Linux servers?

Hi All, How can i run a single command on multiple servers with or without giving credentials. I have a file(servers.txt) which has got list of servers and i want to run a command lsb_release -dr on all these servers and get output of those servers against each server. I tried below code... (9 Replies)
Discussion started by: darling
9 Replies

2. Shell Programming and Scripting

At command for Linux job not working from php

I have a script .sh with a curl command in it: curl --digest "http://xxxxxxx.xxx/mail.php?email=xxxx@xxx.xx"; from terminal this works good : ./script.sh //(mail arrives in my box) from php this works good: shell_exec('./phpsc.sh'); // (mail arrives in my box) from terminal this... (13 Replies)
Discussion started by: extra93
13 Replies

3. UNIX for Dummies Questions & Answers

SWAP command not working on Linux machine

Hi I am working on linux machine and swap command is not working Linux Machine On Solaris machine it is working fine: uname -a SunOS rgsm01 5.9 Generic_118558-03 sun4u sparc SUNW,Sun-Fire-V440 swap -s total: 6596320k bytes allocated + 1035968k reserved = 7632288k used, 38893408k available ... (5 Replies)
Discussion started by: Basant Mishra
5 Replies

4. Red Hat

isql command not working in Linux 64bit

I am using Linux RHEL5 64 bit and installed oracle 11g. I want to check ODBC connection , i had modified odbc.ini file when i use this command -isql I am getting following error . bash: isql: command not found Kindly help (5 Replies)
Discussion started by: roopalidalvi231
5 Replies

5. Shell Programming and Scripting

Sed command issue in linux

I ran one the script in debug mode in linux and have a problem ret='$prmAttunityUser=ais' Now i need to remove $ from this '$prmAttunityUser=ais' so i had added a sed command like this sed 's/$//g' but its not working could you all please help me with an alternate command I want the output... (3 Replies)
Discussion started by: vee_789
3 Replies

6. Shell Programming and Scripting

AWK command working different in Linux

Hi All I have fired a command in linux table=`echo ${file_name} | awk '{FS="/"; print $NF}' | awk '{FS="."; print $1}'` where file_name has /data/ds/dpr_ebicm_uat/backfill/temp/etl_app_info.csv /data/ds/dpr_ebicm_uat/backfill/temp/etl_app_jobs.csv ... (10 Replies)
Discussion started by: vee_789
10 Replies

7. Shell Programming and Scripting

sed command working different in linux environment.

Hi I tried running the code scrname=`whence $0 | sed -e 's/\.\///g'` where $0 is substituted by cm_dsjobrun.sh in unix env then the value it returns me is SCRNAME=/data/ds/dpr_ebicm_uat/etl/cm3_0/scripts/shell/cm_dsjobrun.sh whereas i ran the same code on linux env The value... (9 Replies)
Discussion started by: vee_789
9 Replies

8. Shell Programming and Scripting

ITcl :: try_eval command not working in Linux 5.4

Hi, My code uses try_eval block to do some code execution. This piece of code does not work on RHEL 5.4. The program just hangs at the try_eval block and does not throw any errors either. The same program however works in other Linux boxes that we have. I have written a simple program that... (0 Replies)
Discussion started by: archana485
0 Replies

9. Emergency UNIX and Linux Support

sed on Mac OS versus Linux

I'm trying to replace space with inderscore on Mac OS X 10.6.3 sed -i 's/ /_/g' somefile sed: 1: "hsa_mirbase.fa": extra characters at the end of h command This works perfectly fine on Linux. Thank you Joseph Dhahbi (3 Replies)
Discussion started by: jdhahbi
3 Replies

10. Programming

cos() command not working in Linux

Hi, I have written a c program to find cos() of a value , its not working, I am getting value of "val " as 0000. #include<stdio.h> #include<math.h> main() { float val; val = cosf( 1.570796); printf("\nval = %f",val); } (9 Replies)
Discussion started by: shashi
9 Replies

Featured Tech Videos