Skip to content

Instantly share code, notes, and snippets.

@rodrigobdz
Last active January 28, 2018 20:45
Show Gist options
  • Save rodrigobdz/188d43beeb4db4ab0f78910d80918fcf to your computer and use it in GitHub Desktop.
Save rodrigobdz/188d43beeb4db4ab0f78910d80918fcf to your computer and use it in GitHub Desktop.
Replace line in text file
#!/bin/bash
# Copyright (C) 2017 Rodrigo Bermudez Schettino
# Script to replace a line in a text file
# Create file with sample text
echo "this will be replaced" > file.txt
############
# file.txt #
############
# this will be replaced
LINE_NUMBER=1
CONTENT="foo"
# It takes as an argument the line to replace and the content
# to insert as a replacement
sed -i "${LINE_NUMBER}c\\${CONTENT}" file.txt
############
# file.txt #
############
# foo
@rodrigobdz
Copy link
Author

rodrigobdz commented Jan 28, 2018

Replace line in text file

Example on how to replace a specific line in a file by number.

Usage

# Make script executable
$ chmod +x replace_line.sh
# Run script
$ ./replace_line.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment